PHP version Micro-trust public platform account custom menu class

Source: Internet
Author: User
Tags cdata urlencode

Request custom Menu

The service number may request the custom menu, uses QQ to log in the public number, may upgrade for the mailbox login, uses the mailbox to log in the public number, may modify the login mailbox; The mass message can be synchronized to Tencent Weibo.

Micro-credit Public Platform account API program

The code is as follows Copy Code

<?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-&gt;get_access_token ();//Get Access_token


$this-&gt;createmenu ($access _token);//Create Menu


$this-&gt;delmenu ($access _token);//Delete Menu


$POSTOBJ = simplexml_load_string ($postStr, ' simplexmlelement ', libxml_nocdata);


$this-&gt;fromusername = send a message party ID $POSTOBJ-&gt;fromusername;//


$this-&gt;tousername = $postObj-&gt;tousername;//receive the message party ID


$this-&gt;keyword = Trim ($postObj-&gt;content);//message sent by user


$this-&gt;times = time ()//Date Sent


$MsgType = $POSTOBJ-&gt;msgtype;//Message type


if ($MsgType = = ' event ') {


$MsgEvent = $POSTOBJ-&gt;event;//Get event Type


if ($MsgEvent = = ' Subscribe ') {//Subscribe to Event


$arr [] = "Hello, I am xxx, now we are friends!" Happy [Rose] ";


echo $this-&gt;make_xml ("text", $arr);


Exit


}elseif ($MsgEvent = = ' click ') {//click event


$EventKey = $POSTOBJ The custom key value of the-&gt;eventkey;//menu, which can be used to determine what the user clicked on, thereby pushing different information


$arr [] = $EventKey;


echo $this-&gt;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 ' =&gt;array (


Array


' Name ' =&gt;urlencode ("Life Query"),


' Sub_button ' =&gt;array (


Array


' Name ' =&gt;urlencode ("Weather query"),


' Type ' =&gt; ' click ',


' Key ' =&gt; ' Vcx_weather '


),


Array


' Name ' =&gt;urlencode ("identity card query"),


' Type ' =&gt; ' click ',


' Key ' =&gt; ' vcx_ident '


)


)


),


Array


' Name ' =&gt;urlencode ("Easy Entertainment"),


' Sub_button ' =&gt;array (


Array


' Name ' =&gt;urlencode ("scraping music"),


' Type ' =&gt; ' click ',


' Key ' =&gt; ' Vcx_guahappy '


),


Array


' Name ' =&gt;urlencode ("Lucky Big Wheel"),


' Type ' =&gt; ' click ',


' Key ' =&gt; ' Vcx_luckpan '


)


)


),


Array


' Name ' =&gt;urlencode ("My Information"),


' Sub_button ' =&gt;array (


Array


' Name ' =&gt;urlencode ("About Me"),


' Type ' =&gt; ' click ',


' Key ' =&gt; ' Vcx_aboutme '


),


Array


' Name ' =&gt;urlencode ("Work Information"),


' Type ' =&gt; ' click ',


' Key ' =&gt; ' 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 = "&lt;xml&gt;


&lt;tousername&gt;&lt;! [Cdata[{$this-&gt;fromusername}]]&gt;&lt;/tousername&gt;


&lt;fromusername&gt;&lt;! [Cdata[{$this-&gt;tousername}]]&gt;&lt;/fromusername&gt;


&lt;createtime&gt;{$this-&gt;times}&lt;/createtime&gt;


&lt;msgtype&gt;&lt;! [cdata[{$type}]]&gt;&lt;/msgtype&gt; ";





=================type content============


Switch ($type) {





Case "Text":


$con. = "&lt;content&gt;&lt;! [Cdata[{$value _arr[0]}]]&gt;&lt;/content&gt;


&lt;funcflag&gt;{$o _arr}&lt;/funcflag&gt; ";


Break





Case "News":


$con. = "&lt;articlecount&gt;{$o _arr[0]}&lt;/articlecount&gt;


&lt;Articles&gt; ";


foreach ($value _arr as $id =&gt; $v) {


if ($id &gt;= $o _arr[0]) break; else null; To determine the number of arrays does not exceed the set number


$con. = "&lt;item&gt;


&lt;title&gt;&lt;! [Cdata[{$v [0]}]]&gt;&lt;/title&gt;


&lt;description&gt;&lt;! [Cdata[{$v [1]}]]&gt;&lt;/description&gt;


&lt;picurl&gt;&lt;! [Cdata[{$v [2]}]]&gt;&lt;/picurl&gt;


&lt;url&gt;&lt;! [Cdata[{$v [3]}]]&gt;&lt;/url&gt;


&lt;/item&gt; ";


}


$con. = "&lt;/Articles&gt;


&lt;funcflag&gt;{$o _arr[1]}&lt;/funcflag&gt; ";


Break





}//end Switch





=================end return============


$con. = "&lt;/xml&gt;";





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;


}


}


}

?>

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.