微信图文推送
<?php //微信文档地址 //http://qydev.weixin.qq.com/wiki/index.php?title=消息类型及数据格式 //用户扫码带参数二维码并关注后,微信服务器会根据带参数二维码,访问该接口 function pushmes(){ //获取扫描二维码回传信息 //旧的获取post所有信息的方式,在PHP 5.6后会警告即将废除 //$postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //新的获取post提交的所有信息的方式 $postStr = file_get_contents('php://input'); $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); //发送者账号(openid) $openid = (string)$postObj->FromUserName;//o5-P4snElQIi859NmTB3bZ_ryGF0 //判断扫码前是否已关注微信公众号 $EventKey = trim((string)$postObj->EventKey); $keyArray = explode("_", $EventKey); if (count($keyArray) == 1){ //扫码前已关注微信公众号 //... }else{ //扫码并关注前,该用户未关注微信公众号 //... } //记录日志 $log_filename = "pushmes.log"; $log_content = "\n\r\n\r=====function pushmes ====时间:" . date ( "Y-m-d H:i:s" ) . "======================================================\n\r"; $log_content .= "提交信息====XML:" . $postStr . "====\n\r\n\r\n\r"; $log_content .= "提交信息====openid:" . $openid . "====\n\r"; file_put_contents ( $log_filename, $log_content, FILE_APPEND ); //获取access_token,这里获取方法已经封装,具体怎么获取access_token,查阅资料 $token_res = $this->setrequest("http://vicclub.tech-trans.com.cn/yzc_ui/DataHandler.ashx?method=GetToken"); $access_token = $token_res['Data']['token']; if($access_token){ /* //注释的为推送内容数组格式,微信不会对数组中汉字经json转义后的字符串再反转义成汉字,所以尽量直接写json串,并将汉字直接放在json串中 $postArr = array(); $postArr['touser'] = $openid; $postArr['msgtype'] = "news"; $postArr['news']['articles'] = array(); //如果 $postArr['news']['articles'] 中只有一条信息数组内容,则推送的是单图文 $postArr['news']['articles'][] = array('title'=>'测试第一个标题','description'=>"测试第一个描述","url"=>"www.baidu.com","picurl"=>'http://img1.imgtn.bdimg.com/it/u=3342737063,3964532796&fm=27&gp=0.jpg'); $postArr['news']['articles'][] = array('title'=>'测试第二个标题','description'=>"测试第二个描述","url"=>"www.baidu.com","picurl"=>'http://img1.imgtn.bdimg.com/it/u=3342737063,3964532796&fm=27&gp=0.jpg'); $postArr['news']['articles'][] = array('title'=>'测试第三个标题','description'=>"测试第三个描述","url"=>"www.baidu.com","picurl"=>'http://img1.imgtn.bdimg.com/it/u=3342737063,3964532796&fm=27&gp=0.jpg'); //发送给微信接口前要转成json格式 $postArr = json_encode($postArr); */ //以下为直接合成json串方式 $title1 = '测试第一个标题'; $title2 = '测试第二个标题'; $title3 = '测试第三个标题'; $description1 = '测试第一个描述'; $description2 = '测试第二个描述'; $description3 = '测试第三个描述'; //推送图文的链接地址 $url1 = "www.baidu.com"; $url2 = "www.baidu.com"; $url3 = "www.baidu.com"; //推送图文对应标题显示的图片 $picurl1 = 'http://img1.imgtn.bdimg.com/it/u=3342737063,3964532796&fm=27&gp=0.jpg'; $picurl2 = 'http://img1.imgtn.bdimg.com/it/u=3342737063,3964532796&fm=27&gp=0.jpg'; $picurl3 = 'http://img1.imgtn.bdimg.com/it/u=3342737063,3964532796&fm=27&gp=0.jpg'; //合成json串 $postArr = '{"touser":"'.$openid.'","msgtype":"news","news":{"articles":[{"title":"'.$title1.'","description":"'.$description1.'","url":"'.$url1.'","picurl":"'.$picurl1.'"},{"title":"'.$title2.'","description":"'.$description2.'","url":"'.$url2.'","picurl":"'.$picurl2.'"},{"title":"'.$title3.'","description":"'.$description3.'","picurl":"'.$picurl3.'"}]}}'; //单图文即写一条title即可,例 //$postArr = '{"touser":"'.$openid.'","msgtype":"news","news":{"articles":[{"title":"'.$title1.'","description":"'.$description1.'","url":"'.$url1.'","picurl":"'.$picurl1.'"}]}}'; //开始推送 $res = $this->api_notice_increment("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token, $postArr); echo json_encode(array('status'=>0,'mes'=>'多图文推送成功')); } } function api_notice_increment($url, $data){ $ch = curl_init(); $header = "Accept-Charset: utf-8"; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_AUTOREFERER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $tmpInfo = curl_exec($ch); if (curl_errno($ch)) { curl_close( $ch ); return $ch; }else{ curl_close( $ch ); return $tmpInfo; } } ?>