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

Source: Internet
Author: User

Micro-trust public platform service number can apply for custom menu, the other number temporarily does not support the custom menu, this not only can use the API to operate, but also directly in the background to define menus and parameters oh.

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 upgrade: Service number can request custom menu

wx_menu.php

<?php//define Your token define ("token", "Chenxiang");/change to own token define (' app_id ', '); change to own AppID define (' App_
SECRET ', '); Change to 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;
    The Public function __construct ($appid, $appsecret) {# code ... $this->app_id = $appid;
  $this->app_secret = $appsecret;
    The public function valid () {$echoStr = $_get["Echostr"];
      if ($this->checksignature ()) {echo $echoStr;
    Exit }/** * Run 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); The Public Function responsemsg () {$postStr = $GLOBALS ["Http_raw_post_data"];//returns the 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 ', LI
      Bxml_nocdata); $this->fromusername = $postObj->fromusername;//send the message party ID $this->tousername = $postObj->tousername;//receive the message party ID $this->keyword = Trim ($postObj->content);//The message sent by the user $this->times = time ();//send times $MsgType = $ postobj->msgtype;//Message type if ($MsgType = = ' event ') {$MsgEvent = $POSTOBJ->event;//Gets the event type if ($MsgEv ent== ' 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 A is 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 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 (' name ' =>urlencode (' Life query '), ' Sub_bu
                Tton ' =>array (Array (' Name ' =>urlencode ("Weather Query"), ' type ' => ' click ',
          ' Key ' => ' Vcx_weather '),    Array (' Name ' =>urlencode ("ID query"), ' type ' => ' click ', ' key ' => ' V
            Cx_ident ')), array (' Name ' =>urlencode ("Easy Entertainment"), ' Sub_button ' =>array (Array (' Name ' =>urlencode ("scraping Music"), ' type ' =& gt; ' Click ', ' key ' => ' Vcx_guahappy '), Array (' name ' =>urlenc
              Ode ("Lucky Big Wheel"), ' type ' => ' click ', ' key ' => ' Vcx_luckpan ')
                ), Array (' Name ' =>urlencode ("My Information"), ' Sub_button ' =>array ( Array (' Name ' =>urlencode ("About Me"), ' type ' => ' click ', ' K
                  EY ' => ' 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 type * @param value_arr Array (content), array (ID) * @param o_arr Array ( Title, Introduction, pictures, hyperlinks),... Less than 10), array (number, ID) */Private Function Make_xml ($type, $value _arr, $o _arr=array (0)) {//=================xml Head er============ $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;//judge the number of arrays does not exceed the set number $con. = "<it Em> <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;
 }}}?>

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.