【php開發】實現微信公眾號圖文訊息回復與access_token(程式碼範例)

2020-07-16 10:05:31
本篇文章主要講述的是PHP實現微信公眾號圖文訊息回復與access_token,具有一定的參考價值,感興趣的朋友可以了解一下。

 //程式碼如下
<?php
class IndexAction extends Action {
public function __construct(){

}
public function index(){
//獲得引數 signature nonce token timestamp echostr
$nonce     = $_GET['nonce'];
$token     = 'imooc';
$timestamp = $_GET['timestamp'];
$echostr   = $_GET['echostr'];
$signature = $_GET['signature'];
//形成陣列,然後按字典序排序
$array = array();
$array = array($nonce, $timestamp, $token);
sort($array);
//拼接成字串,sha1加密 ,然後與signature進行校驗
$str = sha1( implode( $array ) );
if( $str  == $signature && $echostr ){
//第一次接入weixin api介面的時候
echo  $echostr;
exit;
}else{
$this->reponseMsg();
}
}
// 接收事件推播並回復
public function reponseMsg(){
//1.獲取到微信推播過來post資料(xml格式)
$postArr = $GLOBALS['HTTP_RAW_POST_DATA'];
//2.處理訊息型別,並設定回復型別和內容
/*<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[FromUser]]></FromUserName>
<CreateTime>123456789</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[subscribe]]></Event>
</xml>*/
$postObj = simplexml_load_string( $postArr );
//$postObj->ToUserName = '';
//$postObj->FromUserName = '';
//$postObj->CreateTime = '';
//$postObj->MsgType = '';
//$postObj->Event = '';
// gh_e79a177814ed
//判斷該封包是否是訂閱的事件推播
if( strtolower( $postObj->MsgType) == 'event'){
//如果是關注 subscribe 事件
if( strtolower($postObj->Event == 'subscribe') ){
//回復使用者訊息(純文字格式)	
$toUser   = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$time     = time();
$msgType  =  'text';
$content  = '歡迎關注我們的微信公眾賬號'.$postObj->FromUserName.'-'.$postObj->ToUserName;
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$info     = sprintf($template, $toUser, $fromUser, $time, $msgType, $content);
echo $info;
/*<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[你好]]></Content>
</xml>*/
}
}
//當微信使用者傳送imooc,公眾賬號回復‘imooc is very good'
/*<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[你好]]></Content>
</xml>*/
/*if(strtolower($postObj->MsgType) == 'text'){
switch( trim($postObj->Content) ){
case 1:
$content = '您輸入的數位是1';
break;
case 2:
$content = '您輸入的數位是2';
break;
case 3:
$content = '您輸入的數位是3';
break;
case 4:
$content = "<a href='http://www.imooc.com'>慕課</a>";
break;
case '英文':
$content = 'imooc is ok';
break;
}	
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
//注意模板中的中括號 不能少 也不能多
$fromUser = $postObj->ToUserName;
$toUser   = $postObj->FromUserName; 
$time     = time();
// $content  = '18723180099';
$msgType  = 'text';
echo sprintf($template, $toUser, $fromUser, $time, $msgType, $content);

}
}
*/
//使用者傳送tuwen1關鍵字的時候,回復一個單圖文
if( strtolower($postObj->MsgType) == 'text' && trim($postObj->Content)=='tuwen2' ){
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$arr = array(
array(
'title'=>'imooc',
'description'=>"imooc is very cool",
'picUrl'=>'https://s3.ap-northeast-1.wasabisys.com/img.tw511.com/202007/logohtyg0ux3hnc.png',
'url'=>'http://www.imooc.com',
),
array(
'title'=>'hao123',
'description'=>"hao123 is very cool",
'picUrl'=>'https://s3.ap-northeast-1.wasabisys.com/img.tw511.com/202007/bdlogomox3gol51df.png',
'url'=>'http://www.hao123.com',
),
array(
'title'=>'qq',
'description'=>"qq is very cool",
'picUrl'=>'https://s3.ap-northeast-1.wasabisys.com/img.tw511.com/202007/logohtyg0ux3hnc.png',
'url'=>'http://www.qq.com',
),
);
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>".count($arr)."</ArticleCount>
<Articles>";
foreach($arr as $k=>$v){
$template .="<item>
<Title><![CDATA[".$v['title']."]]></Title> 
<Description><![CDATA[".$v['description']."]]></Description>
<PicUrl><![CDATA[".$v['picUrl']."]]></PicUrl>
<Url><![CDATA[".$v['url']."]]></Url>
</item>";
}

$template .="</Articles>
</xml> ";
echo sprintf($template, $toUser, $fromUser, time(), 'news');
//注意:進行多圖文傳送時,子圖文個數不能超過10個
}else{
switch( trim($postObj->Content) ){
case 1:
$content = '您輸入的數位是1';
break;
case 2:
$content = '您輸入的數位是2';
break;
case 3:
$content = '您輸入的數位是3';
break;
case 4:
$content = "<a href='http://www.imooc.com'>慕課</a>";
break;
case '英文':
$content = 'imooc is ok';
break;
}	
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
//注意模板中的中括號 不能少 也不能多
$fromUser = $postObj->ToUserName;
$toUser   = $postObj->FromUserName; 
$time     = time();
// $content  = '18723180099';
$msgType  = 'text';
echo sprintf($template, $toUser, $fromUser, $time, $msgType, $content);

}//if end
}//reponseMsg end
function http_curl(){
//獲取imooc
//1.初始化curl
$ch = curl_init();
$url = 'http://www.baidu.com';
//2.設定curl的引數
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//3.採集
$output = curl_exec($ch);
//4.關閉
curl_close($ch);
var_dump($output);
}
function getWxAccessToken(){
//1.請求url地址
$appid = 'wx08d5c2cb632bb5e4';
$appsecret =  '06d3444fb9abd8d00314eb4a38ad61a8';
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
//2初始化
$ch = curl_init();
//3.設定引數
curl_setopt($ch , CURLOPT_URL, $url);
curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
//4.呼叫介面 
$res = curl_exec($ch);
//5.關閉curl
curl_close( $ch );
if( curl_errno($ch) ){
var_dump( curl_error($ch) );
}
$arr = json_decode($res, true);
var_dump( $arr );
}
function getWxServerIp(){
$accessToken = "6vOlKOh7r5uWk_ZPCl3DS36NEK93VIH9Q9tacreuxJ5WzcVc235w_9zONy75NoO11gC9P0o4FBVxwvDiEtsdX6ZRFR0Lfs_ymkb8Bf6kRfo";
$url = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=".$accessToken;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$res = curl_exec($ch);
curl_close($ch);
if(curl_errno($ch)){
var_dump(curl_error($ch));
}
$arr = json_decode($res,true);
echo "<pre>";
var_dump( $arr );
echo "</pre>";

}
}//class end

相關教學:

PHP視訊教學

微信小程式視訊教學

以上就是【php開發】實現微信公眾號圖文訊息回復與access_token(程式碼範例)的詳細內容,更多請關注TW511.COM其它相關文章!