<?php Define your Token Define ("TOKEN", "Chenxiang"); Change to own TOKEN Define (' app_id ', ') change to his own AppID Define (' App_secret ', ') change to his own 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 } } /** * Run the program * @param string $value [description] */ Public Function Run () { $this->responsemsg (); $arr []= "Hello, this is automatic reply, I am not now, something please leave a message, I will reply to your ^_^" as soon as possible; " echo $this->make_xml ("text", $arr); } Public Function responsemsg ()
{
$POSTSTR = $GLOBALS ["Http_raw_post_data"];//return reply data
if (!empty ($POSTSTR)) {
$access _token = $this->get_access_token ();//Get Access_token
$this->createmenu ($access _token);//Create Menu
$this->delmenu ($access _token);//Delete Menu
$POSTOBJ = simplexml_load_string ($postStr, ' simplexmlelement ', libxml_nocdata);
$this->fromusername = send a message party ID $POSTOBJ->fromusername;//
$this->tousername = $postObj->tousername;//receive the message party ID
$this->keyword = Trim ($postObj->content);//message sent by user
$this->times = time ()//Date Sent
$MsgType = $POSTOBJ->msgtype;//Message type
if ($MsgType = = ' event ') {
$MsgEvent = $POSTOBJ->event;//Get event Type
if ($MsgEvent = = ' Subscribe ') {//Subscribe to Event
$arr [] = "Hello, I am xxx, now we are friends!" Happy [Rose] ";
echo $this->make_xml ("text", $arr);
Exit
}elseif ($MsgEvent = = ' click ') {//click event
$EventKey = $POSTOBJ The custom key value of the->eventkey;//menu, which can be used to determine what the user clicked on, thereby pushing different information
$arr [] = $EventKey;
echo $this->make_xml ("text", $arr);
Exit
}
}
}else {
echo "This is a file for Weixin api!";
Exit
}
} /** * Get 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 "Get Access_token error"; } } /**
* Create Menu
* @param $access _token have acquired 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 ("Life Query"),
' Sub_button ' =>array (
Array
' Name ' =>urlencode ("Weather query"),
' Type ' => ' click ',
' Key ' => ' Vcx_weather '
),
Array
' Name ' =>urlencode ("identity card query"),
' Type ' => ' click ',
' Key ' => ' vcx_ident '
)
)
),
Array
' Name ' =>urlencode ("Easy Entertainment"),
' Sub_button ' =>array (
Array
' Name ' =>urlencode ("scraping music"),
' Type ' => ' click ',
' Key ' => ' Vcx_guahappy '
),
Array
' Name ' =>urlencode ("Lucky Big Wheel"),
' Type ' => ' click ',
' Key ' => ' Vcx_luckpan '
)
)
),
Array
' Name ' =>urlencode ("My Information"),
' Sub_button ' =>array (
Array
' Name ' =>urlencode ("About Me"),
' Type ' => ' click ',
' Key ' => ' Vcx_aboutme '
),
Array
' Name ' =>urlencode ("Work Information"),
' 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); } /** * Query Menu * @param $access _token Acquired 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; } /** Delete Menu * @param $access _token Acquired 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 text type, news graphic type
* @param value_arr Array (content), Array (ID)
* @param o_arr Array (title, Introduction, Picture, HYPERLINK),... Less than 10), array (number of bars, 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; To determine the number of arrays does not exceed the set number
$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;
}
}
} ?> |