PHP implementation Micro-trust public platform account custom menu class _php tips

Source: Internet
Author: User

This article describes the implementation of the PHP Micro-trust public platform account custom menu class method. Share to everyone for your reference. The specific analysis is as follows:

The micro-credit public platform service number can be customized menu, the other number does not support the custom menu, this can not only use the API to operate, but also directly in the background to define menus and parameters.

Request custom Menu

Service number can apply for a custom menu, use QQ login public number, you can upgrade to the mailbox login, use the mailbox login public number, you can modify the login mailbox, mass message can be synchronized to Tencent Micro Bo.

Micro-credit public Platform account API program

Copy Code code as follows:
<?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 (!emptyempty ($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 have 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 have 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;


}


}


}


?>

I hope this article will help you with the development of micro-credit based on PHP.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.