您好,登錄后才能下訂單哦!
本篇內容主要講解“微信公眾號php沒有返回信息如何解決”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“微信公眾號php沒有返回信息如何解決”吧!
微信公眾號php沒有返回信息的解決辦法:1、啟用服務器模式;2、通過“define("TOKEN", "weixin");”驗證token;3、新建一個index.php,接收微信返回的數據包并進行處理即可。
微信公眾號php返回信息的實現方法:
php微信公眾號關注后 回復一條文本信息和一條圖文信息
首先還是啟用服務器模式 index.php
驗證token 使用 啟用服務器模式后 把這個index.php 改個名字
下一步:
第二步:新建一個index.php<?php
/**
* wechat php test
* update time: 20141008
*/
//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($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 data
if (!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;
}
}
}
?>
接收微信返回的數據包 進行處理
<?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 data
if( !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沒有返回信息如何解決”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。