本教程操作环境:windows7系统、php8.1版、dell g3电脑。
微信公众号php没有返回信息怎么办?
微信公众号php返回信息的实现方法:
php微信公众号关注后 回复一条文本信息和一条图文信息
首先还是启用服务器模式 index.php
验证token 使用 启用服务器模式后 把这个index.php 改个名字
下一步:
<?php/*** wechat php test* update time: 20141008*///define your tokendefine("token", "weixin");$wechatobj = new wechatcallbackapitest();$wechatobj->valid();class wechatcallbackapitest{public function valid(){$echostr = $_get["echostr"];//valid signature , optionif($this->checksignature()){echo $echostr;exit;}}public function responsemsg(){//get post data, may be due to the different environments//php7 弃用了这个函数 使用file_get_contents('php://input')//$poststr = $globals["http_raw_post_data"];$poststr = file_get_contents('php://input');//extract post dataif (!emptyempty($poststr)){$postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata);$fromusername = $postobj->fromusername;$tousername = $postobj->tousername;$keyword = trim($postobj->content);$time = time();$texttpl = "<xml><tousername><![cdata[%s]]></tousername><fromusername><![cdata[%s]]></fromusername><createtime>%s</createtime><msgtype><![cdata[%s]]></msgtype><content><![cdata[%s]]></content><funcflag>0</funcflag></xml>";if(!emptyempty( $keyword )){$msgtype = "text";$contentstr = "welcome to wechat world!";$resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);echo $resultstr;}else{echo "input something...";}}else {echo "";exit;}}private function checksignature(){$signature = $_get["signature"];$timestamp = $_get["timestamp"];$nonce = $_get["nonce"];$token = token;$tmparr = array($token, $timestamp, $nonce);sort($tmparr, sort_string);$tmpstr = implode( $tmparr );$tmpstr = sha1( $tmpstr );if( $tmpstr == $signature ){return true;}else{return false;}}}?>
第二步:新建一个index.php
接收微信返回的数据包 进行处理
<?php//给微信平台回复以防重复推送和报警ob_clean();//可能在回复echo之前有输出内容,所以先用ob_clean()清空输出缓存区echo "success";session_start();//用于数据error_reporting(0);//关闭php提示报错date_default_timezone_set('prc');//统一设置时区//参数提取和数据查询及保存//获得参数$openid = $_get['openid'];//接收微信平台推送过来的数据包//get post data, may be due to the different environments$poststr = $globals["http_raw_post_data"];//extract post dataif( !empty($poststr) ){$postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata);$tousername = $postobj->tousername;$fromusername = $postobj->fromusername;$msgtype = $postobj->msgtype;$event = $postobj->event;$cardid = $postobj->cardid;$usercardcode = $postobj->usercardcode;$eventkey = $postobj->eventkey;$status = $postobj->status;$content = $postobj->content;}//打开日志文件并写入$date=date("y-m-d");//获取当前日期$txtres = fopen("log/".$date.".txt","a+");$datetime=date("y-m-d h:i:s");//获取当前时间fwrite($txtres,"微信平台事件推送:");fwrite($txtres,"datetime=");fwrite($txtres,$datetime);fwrite($txtres,",openid=");fwrite($txtres,$poststr);fwrite($txtres,",openid=");fwrite($txtres,$openid);fwrite($txtres,",tousername=");fwrite($txtres,$tousername);fwrite($txtres,",fromusername=");fwrite($txtres,$fromusername);fwrite($txtres,",msgtype=");fwrite($txtres,$msgtype);fwrite($txtres,",event=");fwrite($txtres,$event);fwrite($txtres,",cardid=");fwrite($txtres,$cardid);fwrite($txtres,",usercardcode=");fwrite($txtres,$usercardcode);fwrite($txtres,",eventkey=");fwrite($txtres,$eventkey);fwrite($txtres,",status=");fwrite($txtres,$status);fwrite($txtres,",content=");fwrite($txtres,$content);fwrite($txtres,"\r\n");fclose($txtres);//关闭指针//获取access_token$appid = "";$secret = "";$token_url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;$json=file_get_contents($token_url);//echo "<pre>";//print_r($json);//echo "</pre>";$result=json_decode($json,true);$access_token=$result['access_token'];// $access_token;//关注回复if( $event=="subscribe" ){ $texttpl = "<xml><tousername><![cdata[%s]]></tousername><fromusername><![cdata[%s]]></fromusername><createtime>%s</createtime><msgtype><![cdata[%s]]></msgtype><content><![cdata[%s]]></content><funcflag>0</funcflag></xml>"; $time = time(); //时间戳 $msgtype = 'text'; //消息类型:文本 // $contentstr = "这是文本信息"; //$contentstr = preg_replace("#\\\u([0-9a-f]+)#ie","iconv('ucs-2','utf-8', pack('h4', '\\1'))",$contentstr);//对emoji unicode进行二进制pack并转utf8 $resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr); echo $resultstr;//打开日志文件并写入 $date=date("y-m-d");//获取当前日期 $txtres = fopen("log/".$date.".txt","a+"); $datetime=date("y-m-d h:i:s");//获取当前时间 fwrite($txtres,"文本:"); fwrite($txtres,"datetime=");fwrite($txtres,$datetime); fwrite($txtres,",resultstr=");fwrite($txtres,$resultstr); fwrite($txtres,"\r\n"); fclose($txtres);//关闭指针 function https_request($url,$data = null){ $curl = curl_init(); curl_setopt($curl, curlopt_url, $url); curl_setopt($curl, curlopt_ssl_verifypeer, false); curl_setopt($curl, curlopt_ssl_verifyhost, false); if (!empty($data)){ curl_setopt($curl, curlopt_post, 1); curl_setopt($curl, curlopt_postfields, $data); } curl_setopt($curl, curlopt_returntransfer, 1); $output = curl_exec($curl); curl_close($curl); return $output; }//图文信息//图文信息 id 调用微信接口 查询素材内容 就好 $media_id='edjonlffwfqp8tjrkdcce-iuzkwaokjx8tbiqknzelw'; $url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$access_token; $data = '{ "touser":"'.$fromusername.'", "msgtype":"mpnews", "mpnews": { "media_id":"'.$media_id.'" } }'; $result = https_request($url,$data); //打开日志文件并写入 $date=date("y-m-d");//获取当前日期 $txtres = fopen("log/".$date.".txt","a+"); $datetime=date("y-m-d h:i:s");//获取当前时间 fwrite($txtres,"图文:"); fwrite($txtres,"datetime=");fwrite($txtres,$datetime); fwrite($txtres,",data=");fwrite($txtres,$data); fwrite($txtres,"\r\n"); fwrite($txtres,",resultstr=");fwrite($txtres,$result); fwrite($txtres,"\r\n"); fclose($txtres);//关闭指针}?>
多图文和一条文本信息
多图文和一条文本信息
因为客服回复消息 只能是一条图文 不然会报错
/** 多图文和一条文本信息* 多图文和一条文本信息* 因为客服回复消息 只能是一条图文 不然会报错/ //多图文信息 $time = time(); //$media_id='efabfnhphshvpcfnyhdt0dtuui-pla_nvzsyplubb'; $title = "图文标题"; $url = "图文路劲"; $thumb_url ="封面图"; $digest ="说明"; $title1 = ""; $url1 = ""; $thumb_url1 =""; $digest1 = ""; $title2 = ""; $url2 = ""; $thumb_url2 =""; $digest2 = "";... $title7 = "图文标题"; $url7 = "图文路劲"; $thumb_url7 = "封面图"; $digest7 = "说明"; $texttpl = "<xml> <tousername><![cdata[%s]]></tousername> <fromusername><![cdata[%s]]></fromusername> <createtime>%s</createtime> <msgtype><![cdata[%s]]></msgtype> <articlecount><![cdata[%s]]></articlecount> <articles> <item> <title><![cdata[%s]]></title> <description><![cdata[%s]]></description> <picurl><![cdata[%s]]></picurl> <url><![cdata[%s]]></url> </item> <item> <title><![cdata[%s]]></title> <description><![cdata[%s]]></description> <picurl><![cdata[%s]]></picurl> <url><![cdata[%s]]></url> </item> <item> <title><![cdata[%s]]></title> <description><![cdata[%s]]></description> <picurl><![cdata[%s]]></picurl> <url><![cdata[%s]]></url> </item> <item> <title><![cdata[%s]]></title> <description><![cdata[%s]]></description> <picurl><![cdata[%s]]></picurl> <url><![cdata[%s]]></url> </item> <item> <title><![cdata[%s]]></title> <description><![cdata[%s]]></description> <picurl><![cdata[%s]]></picurl> <url><![cdata[%s]]></url> </item> <item> <title><![cdata[%s]]></title> <description><![cdata[%s]]></description> <picurl><![cdata[%s]]></picurl> <url><![cdata[%s]]></url> </item> <item> <title><![cdata[%s]]></title> <description><![cdata[%s]]></description> <picurl><![cdata[%s]]></picurl> <url><![cdata[%s]]></url> </item> <item> <title><![cdata[%s]]></title> <description><![cdata[%s]]></description> <picurl><![cdata[%s]]></picurl> <url><![cdata[%s]]></url> </item> </articles> </xml>"; $resultstr = sprintf($texttpl,$fromusername,$tousername,$time,'news','8',$title,$digest,$thumb_url,$url,$title,$digest,$thumb_url,$url,$title,$digest,$thumb_url,$url,$title,$digest,$thumb_url,$url,$title,$digest,$thumb_url,$url,$title,$digest,$thumb_url,$url,$title,$digest,$thumb_url,$url,$title,$digest,$thumb_url,$url); echo $resultstr; //news 表示图文//8 表示有几个图文 8个图文 后面就要写 $title,$digest,$thumb_url,$url 这样的数据//xml 格式 里面的 <item>...</item> 有几个就写几个 $url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$access_token; $data = '{ "touser":"'.$fromusername.'", "msgtype":"text", "text": { "content":"这是文本内容" } }'; $result = https_request($url,$data);
推荐学习:《php视频教程》
以上就是微信公众号php没有返回信息怎么办的详细内容。
