[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);
}