微信小程序服务消息推送

微信小程序服务消息推送

<?php
define("APPId", "你的appid");
define("SECRET", "你的secret");
define("URL", "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential"); //获取access_token
define("SENDURL", "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?");//发送服务消息


/**
 * 
 */
class SendMessage
{
		private $AppId;
		private $Secret;
		private $access_token;
		private $url ;
		private $sendUrl ;
	
	function __construct()
	{
			$this->AppId = APPId;
			$this->Secret = SECRET;
			$this->sendUrl = SENDURL;
			$this->url = URL."&appid=".$this->AppId."&secret=".$this->Secret;

	}

    /**
     * @return token 成功返回access_token 失败false
     */
	private function getAccessToken(){
		$res = $this->curl_post($this->url, '');
		if(!$res){
			return false;
		}
		$data = json_decode($res,true);
		$access_token =  isset($data["access_token"]) ?$data["access_token"] : false;
		return $access_token;
	}

    /**
     * @return string 返回完整的url 或者 false
     */
	private function getSendUrl(){

		if($this->getAccessToken()){
			$this->sendUrl = $this->sendUrl."access_token=".$this->getAccessToken();
			return $this->sendUrl;
		}else{
			return false;
		}
		
		
	}

    /**
     * 订单取消发送服务消息
     * @param $data 订单信息 和form_id (表单提交场景下,为 submit 事件带上的 formId;支付场景下,为本次支付的 prepay_id)
     * @param  touser openid
     * @param  template_id 模板id
     * @param  form_id 表单提交场景下,为 submit 事件带上的 formId;支付场景下,为本次支付的 prepay_id
     * @param  emphasis_keyword 模板需要放大的关键词,不填则默认无放大
     * @return array
     */
	public function sendCancelOrder($data){
		$result = array("code"=>200,"msg"=>"");
			$template = '{
			  "touser": "oJc2H5MkFVnCRBX3SyMjQ-YxvNlg", 
			  "template_id": "uw1l_2c5qpa40rtciZfEJhda1LePxTjuxqebpNf87rY",
			  "page": "home2",
			  "form_id": "90ad4219d3a24f89a288ea3bfdb72405",
			  "data": {
			      "keyword1": {
			          "value": "45465476"
			      },
			      "keyword2": {
			          "value": "2019年06月18日 11:30"
			      },
			      "keyword3": {
			          "value": "20.00"
			      } ,
			      "keyword4": {
			          "value": "2019年06月18日 11:30"
			      },
			      "keyword5": {
			          "value": "测试测试"
			      },
			      "keyword6": {
			          "value": "测试"
			      },
			      "keyword7": {
			          "value": "优品驾到"
			      }
			      ,"keyword8": {
			          "value": "测试"
			      }
			  },
			  "emphasis_keyword": ""
			}';

			$url  = $this->getSendUrl();
			if(!$url){
				$result["code"] = 400;
				$result["msg"] = "发送服务消息地址为空";
				return $result;

			}
			$data = $this->curl_post($url,$template );
			$data = json_decode($data,true);
			if($data["errcode"] == 0 && $data["errmsg"] == "ok"){
				$result["msg"] = "发送消息成功";
			}else{
				$result["code"] = 400;
				$result["errmsg"] = $data["errmsg"];
			}
            return $result;
	}

    /**
     * @param $url
     * @param $fields
     * @param string $data_type
     * @return bool|mixed|string
     */
	public function curl_post($url, $fields, $data_type='text'){
        $cl = curl_init();
        if(stripos($url, 'https://') !== FALSE) {
            curl_setopt($cl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($cl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($cl, CURLOPT_SSLVERSION, 1);
        }
        curl_setopt($cl, CURLOPT_URL, $url);
        curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt($cl, CURLOPT_POST, true);    
        curl_setopt($cl, CURLOPT_POSTFIELDS, $fields);
        $content = curl_exec($cl);
        $status = curl_getinfo($cl);
        curl_close($cl);
        if (isset($status['http_code']) && $status['http_code'] == 200) {
            if ($data_type == 'json') {
                $content = json_decode($content);
            }
            return $content;
        } else {
            return FALSE;
        }

    }


}


include_once "SendMessage.php";
 $msg =new SendMessage();
 $data = array();
 $res = $msg->sendCancelOrder( $data);
 echo "<pre>";
 var_dump($res);

模板消息参考链接和注意事项

posted @ 2019-09-16 09:51  星光下  阅读(831)  评论(0编辑  收藏  举报