微信模板消息调用
程序员文章站
2022-03-11 17:20:06
...
[PHP]代码
/* * Curl 请求 */ public function wtw_request($url,$data=null){ $curl = curl_init(); // 启动一个CURL会话 curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在 curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器 if($data != null){ curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求 curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包 } curl_setopt($curl, CURLOPT_TIMEOUT, 300); // 设置超时限制防止死循环 curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回 $info = curl_exec($curl); // 执行操作 if (curl_errno($curl)) { echo 'Errno:'.curl_getinfo($curl);//捕抓异常 dump(curl_getinfo($curl)); } return $info; } /* * 模板消息 */ public function templetemsg(){ //获取ACCESS_TOKEN /*string(86) "*****" * string(45) "{"errcode":0,"errmsg":"ok","msgid":201652404}" */ $ACCESS_TOKEN = $this->get_Token(); $msg_url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$ACCESS_TOKEN.""; $time = date("Y-m-d H:i:s",time()); $url ="http://www。baidu.com/"; ////这个链接是点击图文 跳转的链接,换行只能用\n 不能用<Br/> ////请求包为一个json: $msg_json= '{ "touser":"oPLOfjqw1fKtdUQPk1vDXCf7K8s0", "template_id":"iZOHjFSa-Ax2UuDlzjJx6IrwUrSvdDhD4TQPnM8kFSY", "url":"'.$url.'", "topcolor":"#FF0000", "data":{ "first":{ "value":"尊敬的先生:\n恭喜你模板接口调用成功了\n\n", "color":"#000000" }, "keyword1":{ "value":"模板接口调用测试\n\n", "color":"#000000" }, "keyword2":{ "value":"'.$time.'\n\n", "color":"#000000" }, "remark":{ "value":"\n\nThis message is intended only for the use of the inpidual or entity激活成功", "color":"#0099FF" } } }' ; $result = $this->wtw_request($msg_url,$msg_json); ///$result = $this->msg_request($msg_url, $msg_json, $header = array()); dump($result); }