php實(shí)現(xiàn)微信公眾平臺(tái)賬號(hào)自定義菜單類
本文實(shí)例講述了php實(shí)現(xiàn)微信公眾平臺(tái)賬號(hào)自定義菜單類的方法。分享給大家供大家參考。具體分析如下:
微信公眾平臺(tái)服務(wù)號(hào)可申請(qǐng)自定義菜單了,其它的號(hào)暫時(shí)不支持自定義菜單了,這個(gè)不但可以使用api來(lái)操作,還可以直接在后臺(tái)定義菜單與參數(shù)。
申請(qǐng)自定義菜單
服務(wù)號(hào)可以申請(qǐng)自定義菜單,使用QQ登錄的公眾號(hào),可以升級(jí)為郵箱登錄,使用郵箱登錄的公眾號(hào),可以修改登錄郵箱,群發(fā)消息可以同步到騰訊微博.
微信公眾平臺(tái)賬號(hào)api程序
//define your token
define("TOKEN", "chenxiang");//改成自己的TOKEN
define('APP_ID', '');//改成自己的APPID
define('APP_SECRET', '');//改成自己的APPSECRET
$wechatObj = new wechatCallbackapiTest(APP_ID,APP_SECRET);
$wechatObj->Run();
class wechatCallbackapiTest
{
private $fromUsername;
private $toUsername;
private $times;
private $keyword;
private $app_id;
private $app_secret;
public function __construct($appid,$appsecret)
{
# code...
$this->app_id = $appid;
$this->app_secret = $appsecret;
}
public function valid()
{
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
/**
* 運(yùn)行程序
* @param string $value [description]
*/
public function Run()
{
$this->responseMsg();
$arr[]= "您好,這是自動(dòng)回復(fù),我現(xiàn)在不在,有事請(qǐng)留言,我會(huì)盡快回復(fù)你的^_^";
echo $this->make_xml("text",$arr);
}
public function responseMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];//返回回復(fù)數(shù)據(jù)
if (!emptyempty($postStr)){
$access_token = $this->get_access_token();//獲取access_token
$this->createmenu($access_token);//創(chuàng)建菜單
//$this->delmenu($access_token);//刪除菜單
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$this->fromUsername = $postObj->FromUserName;//發(fā)送消息方ID
$this->toUsername = $postObj->ToUserName;//接收消息方ID
$this->keyword = trim($postObj->Content);//用戶發(fā)送的消息
$this->times = time();//發(fā)送時(shí)間
$MsgType = $postObj->MsgType;//消息類型
if($MsgType=='event'){
$MsgEvent = $postObj->Event;//獲取事件類型
if ($MsgEvent=='subscribe') {//訂閱事件
$arr[] = "你好,我是xxx,現(xiàn)在我們是好友咯![愉快][玫瑰]";
echo $this->make_xml("text",$arr);
exit;
}elseif ($MsgEvent=='CLICK') {//點(diǎn)擊事件
$EventKey = $postObj->EventKey;//菜單的自定義的key值,可以根據(jù)此值判斷用戶點(diǎn)擊了什么內(nèi)容,從而推送不同信息
$arr[] = $EventKey;
echo $this->make_xml("text",$arr);
exit;
}
}
}else {
echo "this a file for weixin API!";
exit;
}
}
/**
* 獲取access_token
*/
private function get_access_token()
{
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->app_id."&secret=".$this->app_secret;
$data = json_decode(file_get_contents($url),true);
if($data['access_token']){
return $data['access_token'];
}else{
return "獲取access_token錯(cuò)誤";
}
}
/**
* 創(chuàng)建菜單
* @param $access_token 已獲取的ACCESS_TOKEN
*/
public function createmenu($access_token)
{
$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;
$arr = array(
'button' =>array(
array(
'name'=>urlencode("生活查詢"),
'sub_button'=>array(
array(
'name'=>urlencode("天氣查詢"),
'type'=>'click',
'key'=>'VCX_WEATHER'
),
array(
'name'=>urlencode("身份證查詢"),
'type'=>'click',
'key'=>'VCX_IDENT'
)
)
),
array(
'name'=>urlencode("輕松娛樂(lè)"),
'sub_button'=>array(
array(
'name'=>urlencode("刮刮樂(lè)"),
'type'=>'click',
'key'=>'VCX_GUAHAPPY'
),
array(
'name'=>urlencode("幸運(yùn)大轉(zhuǎn)盤(pán)"),
'type'=>'click',
'key'=>'VCX_LUCKPAN'
)
)
),
array(
'name'=>urlencode("我的信息"),
'sub_button'=>array(
array(
'name'=>urlencode("關(guān)于我"),
'type'=>'click',
'key'=>'VCX_ABOUTME'
),
array(
'name'=>urlencode("工作信息"),
'type'=>'click',
'key'=>'VCX_JOBINFORMATION'
)
)
)
)
);
$jsondata = urldecode(json_encode($arr));
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$jsondata);
curl_exec($ch);
curl_close($ch);
}
/**
* 查詢菜單
* @param $access_token 已獲取的ACCESS_TOKEN
*/
private function getmenu($access_token)
{
# code...
$url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=".$access_token;
$data = file_get_contents($url);
return $data;
}
/**
* 刪除菜單
* @param $access_token 已獲取的ACCESS_TOKEN
*/
private function delmenu($access_token)
{
# code...
$url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".$access_token;
$data = json_decode(file_get_contents($url),true);
if ($data['errcode']==0) {
# code...
return true;
}else{
return false;
}
}
/**
*@param type: text 文本類型, news 圖文類型
*@param value_arr array(內(nèi)容),array(ID)
*@param o_arr array(array(標(biāo)題,介紹,圖片,超鏈接),...小于10條),array(條數(shù),ID)
*/
private function make_xml($type,$value_arr,$o_arr=array(0)){
//=================xml header============
$con="<xml>
<ToUserName><![CDATA[{$this->fromUsername}]]></ToUserName>
<FromUserName><![CDATA[{$this->toUsername}]]></FromUserName>
<CreateTime>{$this->times}</CreateTime>
<MsgType><![CDATA[{$type}]]></MsgType>";
//=================type content============
switch($type){
case "text" :
$con.="<Content><![CDATA[{$value_arr[0]}]]></Content>
<FuncFlag>{$o_arr}</FuncFlag>";
break;
case "news" :
$con.="<ArticleCount>{$o_arr[0]}</ArticleCount>
<Articles>";
foreach($value_arr as $id=>$v){
if($id>=$o_arr[0]) break; else null; //判斷數(shù)組數(shù)不超過(guò)設(shè)置數(shù)
$con.="<item>
<Title><![CDATA[{$v[0]}]]></Title>
<Description><![CDATA[{$v[1]}]]></Description>
<PicUrl><![CDATA[{$v[2]}]]></PicUrl>
<Url><![CDATA[{$v[3]}]]></Url>
</item>";
}
$con.="</Articles>
<FuncFlag>{$o_arr[1]}</FuncFlag>";
break;
} //end switch
//=================end return============
$con.="</xml>";
return $con;
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
?>
希望本文所述對(duì)大家基于PHP的微信開(kāi)發(fā)有所幫助。
- php版微信公眾平臺(tái)之微信網(wǎng)頁(yè)登陸授權(quán)示例
- PHP實(shí)現(xiàn)微信網(wǎng)頁(yè)授權(quán)開(kāi)發(fā)教程
- 微信網(wǎng)頁(yè)授權(quán)(OAuth2.0) PHP 源碼簡(jiǎn)單實(shí)現(xiàn)
- weiphp微信公眾平臺(tái)授權(quán)設(shè)置
- PHP實(shí)現(xiàn)微信小程序用戶授權(quán)的工具類示例
- 基于php的微信公眾平臺(tái)開(kāi)發(fā)入門(mén)實(shí)例
- php判斷頁(yè)面是否是微信打開(kāi)的示例(微信打開(kāi)網(wǎng)頁(yè))
- PHP對(duì)接微信公眾平臺(tái)消息接口開(kāi)發(fā)流程教程
- PHP版微信第三方實(shí)現(xiàn)一鍵登錄及獲取用戶信息的方法
- PHP微信網(wǎng)頁(yè)授權(quán)的配置文件操作分析
相關(guān)文章
PHP中使用gettext來(lái)支持多語(yǔ)言的方法
開(kāi)發(fā)多語(yǔ)言的Web應(yīng)用是一件非常困難的事,各個(gè)國(guó)家的字符集的編碼方式、貨幣符號(hào)、日期格式、數(shù)字格式、文字表現(xiàn)都各不相同.2011-05-05編譯php 5.2.14+fpm+memcached(具體操作詳解)
本篇文章是對(duì)編譯php5.2.14+fpm+memcached的具體操作進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06PHP實(shí)現(xiàn)的注冊(cè),登錄及查詢用戶資料功能API接口示例
這篇文章主要介紹了PHP實(shí)現(xiàn)的注冊(cè),登錄及查詢用戶資料功能API接口,結(jié)合具體實(shí)例形式分析了php服務(wù)器端用戶注冊(cè)、登陸、查詢及curl登陸查詢功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-06-06