微信公眾號php沒有返回資訊的解決辦法:1、啟用伺服器模式;2、通過「define("TOKEN", "weixin");」驗證token;3、新建一個index.php,接收微信返回的封包並進行處理即可。
php入門到就業線上直播課:進入學習
Apipost = Postman + Swagger + Mock + Jmeter 超好用的API偵錯工具:
本教學操作環境:Windows7系統、PHP8.1版、Dell G3電腦。
微信公眾號php沒有返回資訊怎麼辦?
微信公眾號php返回資訊的實現方法:
php微信公眾號關注後 回覆一條文字資訊和一條圖文資訊
首先還是啟用伺服器模式 index.php
驗證token 使用 啟用伺服器模式後 把這個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;
}
}
}
?>
登入後複製
第二步:新建一個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 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沒有返回資訊怎麼辦的詳細內容,更多請關注TW511.COM其它相關文章!