微信小程序发送订阅消息/PHP微信小程序发送订阅消息

helei 2020-6-9 635 6/9

1.微信唤起订阅消息

 wx.requestSubscribeMessage({

      tmplIds: ['****'],
      success (res) {
        console.log(res)
      }
    })

2.后台服务器发送消息

if(isset($_GET['msg'])){
		$url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=".get_token();
		echo get_token();
		$data_string = '{
		"touser": "openid",
		"template_id": "template_id",
		"page": "index/index",
		"miniprogram_state":"developer",
		"lang":"zh_CN",
		"data": {
			"thing1": {
				"value": "哈哈哈哈哈哈"
			},
			"character_string2": {
				"value": "2019/10/23 15:00 ~ 17:00"
			}
		  }
		}';
		var_dump(http_post($url,$data_string));
	}
	
	function get_token(){
		$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=小程序appid&secret=小程序secret";
		$res = http_get($url);
		$token = json_decode($res)->access_token;
		return $token;
	}
	function http_post($url,$data_string,$timeout = 60)
    {
        //curl验证成功
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//// 跳过证书检查 
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string)
        ));
  
        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            print curl_error($ch);
        }
        curl_close($ch);
        return $result;
    }

微信文档地址

- THE END -

helei

9月23日16:21

最后修改:2020年9月23日
0