本篇內(nèi)容主要講解“微信公眾號(hào)php沒(méi)有返回信息如何解決”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“微信公眾號(hào)php沒(méi)有返回信息如何解決”吧!
創(chuàng)新互聯(lián)專注于企業(yè)營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站重做改版、介休網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5、商城網(wǎng)站定制開(kāi)發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為介休等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
微信公眾號(hào)php沒(méi)有返回信息的解決辦法:1、啟用服務(wù)器模式;2、通過(guò)“define("TOKEN", "weixin");”驗(yàn)證token;3、新建一個(gè)index.php,接收微信返回的數(shù)據(jù)包并進(jìn)行處理即可。
微信公眾號(hào)php返回信息的實(shí)現(xiàn)方法:
php微信公眾號(hào)關(guān)注后 回復(fù)一條文本信息和一條圖文信息
首先還是啟用服務(wù)器模式 index.php
驗(yàn)證token 使用 啟用服務(wù)器模式后 把這個(gè)index.php 改個(gè)名字
下一步:
第二步:新建一個(gè)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 棄用了這個(gè)函數(shù) 使用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;
}
}
}
?>
接收微信返回的數(shù)據(jù)包 進(jìn)行處理
<?php
//給微信平臺(tái)回復(fù)以防重復(fù)推送和報(bào)警
ob_clean();//可能在回復(fù)echo之前有輸出內(nèi)容,所以先用ob_clean()清空輸出緩存區(qū)
echo "success";
session_start();//用于數(shù)據(jù)
error_reporting(0);//關(guān)閉php提示報(bào)錯(cuò)
date_default_timezone_set('PRC');//統(tǒng)一設(shè)置時(shí)區(qū)
//參數(shù)提取和數(shù)據(jù)查詢及保存
//獲得參數(shù)
$openid = $_GET['openid'];
//接收微信平臺(tái)推送過(guò)來(lái)的數(shù)據(jù)包
//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;
}
//打開(kāi)日志文件并寫入
$date=date("Y-m-d");//獲取當(dāng)前日期
$TxtRes = fopen("log/".$date.".txt","a+");
$datetime=date("Y-m-d H:i:s");//獲取當(dāng)前時(shí)間
fwrite($TxtRes,"微信平臺(tái)事件推送:");
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);//關(guān)閉指針
//獲取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;
//關(guān)注回復(fù)
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(); //時(shí)間戳
$msgType = 'text'; //消息類型:文本
//
$contentStr = "這是文本信息";
//$contentStr = preg_replace("#\\\u([0-9a-f]+)#ie","iconv('UCS-2','UTF-8', pack('H4', '\\1'))",$contentStr);//對(duì)emoji unicode進(jìn)行二進(jìn)制pack并轉(zhuǎn)utf8
$resultStr = sprintf($textTpl, $fromUserName, $toUserName, $time, $msgType, $contentStr);
echo $resultStr;
//打開(kāi)日志文件并寫入
$date=date("Y-m-d");//獲取當(dāng)前日期
$TxtRes = fopen("log/".$date.".txt","a+");
$datetime=date("Y-m-d H:i:s");//獲取當(dāng)前時(shí)間
fwrite($TxtRes,"文本:");
fwrite($TxtRes,"datetime=");fwrite($TxtRes,$datetime);
fwrite($TxtRes,",resultStr=");fwrite($TxtRes,$resultStr);
fwrite($TxtRes,"\r\n");
fclose($TxtRes);//關(guān)閉指針
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 調(diào)用微信接口 查詢素材內(nèi)容 就好
$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);
//打開(kāi)日志文件并寫入
$date=date("Y-m-d");//獲取當(dāng)前日期
$TxtRes = fopen("log/".$date.".txt","a+");
$datetime=date("Y-m-d H:i:s");//獲取當(dāng)前時(shí)間
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);//關(guān)閉指針
}
?>
多圖文和一條文本信息
多圖文和一條文本信息
因?yàn)榭头貜?fù)消息 只能是一條圖文 不然會(huì)報(bào)錯(cuò)
/*
* 多圖文和一條文本信息
* 多圖文和一條文本信息
* 因?yàn)榭头貜?fù)消息 只能是一條圖文 不然會(huì)報(bào)錯(cuò)
/
//多圖文信息
$time = time();
//$media_id='EFAbfNhphshVpCfNyhdT0dtUui-pLa_NvzSyPLuBb';
$title = "圖文標(biāo)題";
$url = "圖文路勁";
$thumb_url ="封面圖";
$digest ="說(shuō)明";
$title1 = "";
$url1 = "";
$thumb_url1 ="";
$digest1 = "";
$title2 = "";
$url2 = "";
$thumb_url2 ="";
$digest2 = "";
.
.
.
$title7 = "圖文標(biāo)題";
$url7 = "圖文路勁";
$thumb_url7 = "封面圖";
$digest7 = "說(shuō)明";
$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 表示有幾個(gè)圖文 8個(gè)圖文 后面就要寫 $title,$digest,$thumb_url,$url 這樣的數(shù)據(jù)
//xml 格式 里面的 <item>...</item> 有幾個(gè)就寫幾個(gè)
$url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$ACCESS_TOKEN;
$data = '{
"touser":"'.$fromUserName.'",
"msgtype":"text",
"text":
{
"content":"這是文本內(nèi)容"
}
}';
$result = https_request($url,$data);
到此,相信大家對(duì)“微信公眾號(hào)php沒(méi)有返回信息如何解決”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
分享題目:微信公眾號(hào)php沒(méi)有返回信息如何解決
分享鏈接:http://jinyejixie.com/article20/ggidco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、網(wǎng)站營(yíng)銷、商城網(wǎng)站、微信小程序、網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
網(wǎng)頁(yè)設(shè)計(jì)公司知識(shí)