请求地址:https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=ACCESS_TOKEN
post发送消息体如下:
$url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token={$accessToken}"; $sendData = [ "access_token" => $accessToken,//用户平台的appid跟appsecret "touser" => $openid,//用户平台获取的openid "mp_template_msg" => [ "appid" => $this->pubAppId,//公众号id "template_id" => $this->template_id,//模板id 'url' => "http://" . $_SERVER['SERVER_NAME'], "miniprogram" => [//如果跳转小程序的话需要的小程序appid跟路径,小程序需要跟公众号关联并且主体保持一致 "appid" => $this->miniprogramAppid, "page" => "/pages/index/index", ], "data" => [ "first" => ["value" => "这是测试"], "keyword1" => ["value" => "会议名称"], "keyword2" => ["value" => "会议时间"], "keyword3" => ["value" => "会议地点"], "keyword4" => ["value" => "会议介绍"], "remark" => ["value" => "请及时处理!"], ], ], ];
注意:
touser的值与access_token的值是通过同一套appid跟appsecret
举例:如果你的客户端是小程序,touser的值就是小程序获取到的用户openid,access_token也需要用小程序的appid和appsecret来获取
否则会报错(无效的openid)
另外如果确定参数配置都没问题,但是一直报错的话,建议检查一下curl,我开始用的另一套请求方式,一直报错,换了另一套就对了,还是记录一下
function send_post($url, $param) { if (empty($url) || empty($param)) { return false; } $postUrl = $url; $curlPost = json_encode($param); $ch = curl_init();//初始化curl curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页 curl_setopt($ch, CURLOPT_HEADER, 0);//设置header curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上 curl_setopt($ch, CURLOPT_POST, 1);//post提交方式 curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); $data = curl_exec($ch);//运行curl curl_close($ch); return $data; }