亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

php實(shí)現(xiàn)微信公眾平臺(tái)賬號(hào)自定義菜單類

 更新時(shí)間:2015年10月11日 15:36:32   投稿:hebedich  
這篇文章主要介紹了php實(shí)現(xiàn)微信公眾平臺(tái)賬號(hào)自定義菜單類的相關(guān)資料,需要的朋友可以參考下

微信公眾平臺(tái)服務(wù)號(hào)可申請(qǐng)自定義菜單了,其它的號(hào)暫時(shí)不支持自定義菜單了,這個(gè)不但可以使用api來(lái)操作,還可以直接在后臺(tái)定義菜單與參數(shù)哦。

服務(wù)號(hào)可以申請(qǐng)自定義菜單;使用QQ登錄的公眾號(hào),可以升級(jí)為郵箱登錄;使用郵箱登錄的公眾號(hào),可以修改登錄郵箱;群發(fā)消息可以同步到騰訊微博。微信公眾平臺(tái)升級(jí):服務(wù)號(hào)可申請(qǐng)自定義菜單

wx_menu.php

<?php
//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 (!empty($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;
    }
  }
}
?>

相關(guān)文章

  • php.ini中date.timezone設(shè)置分析

    php.ini中date.timezone設(shè)置分析

    date.timezone設(shè)置php5默認(rèn)date.timezone為utc,改為date.timezone = PRC即可解決時(shí)間相差八小時(shí)的問(wèn)題,但我在php的官方文檔中看了半天也沒(méi)找到這個(gè)參數(shù)啊
    2011-07-07
  • PHP實(shí)現(xiàn)正則表達(dá)式分組捕獲操作示例

    PHP實(shí)現(xiàn)正則表達(dá)式分組捕獲操作示例

    這篇文章主要介紹了PHP實(shí)現(xiàn)正則表達(dá)式分組捕獲操作,結(jié)合實(shí)例形式分析了php正則表達(dá)式獲取分組捕獲操作的相關(guān)實(shí)現(xiàn)方法與使用注意事項(xiàng),需要的朋友可以參考下
    2018-02-02
  • php環(huán)境下利用session防止頁(yè)面重復(fù)刷新的具體實(shí)現(xiàn)

    php環(huán)境下利用session防止頁(yè)面重復(fù)刷新的具體實(shí)現(xiàn)

    如何防止頁(yè)面重復(fù)刷新,在php環(huán)境下可以利用session來(lái)輕松實(shí)現(xiàn),下面是具體的代碼,需要的朋友可以參考下
    2014-01-01
  • PHP實(shí)現(xiàn)的簡(jiǎn)單四則運(yùn)算計(jì)算器功能示例

    PHP實(shí)現(xiàn)的簡(jiǎn)單四則運(yùn)算計(jì)算器功能示例

    這篇文章主要介紹了PHP實(shí)現(xiàn)的簡(jiǎn)單四則運(yùn)算計(jì)算器功能,結(jié)合實(shí)例形式分析了PHP基于堆棧實(shí)現(xiàn)的表達(dá)式運(yùn)算功能,需要的朋友可以參考下
    2017-12-12
  • MYSQL環(huán)境變量設(shè)置方法

    MYSQL環(huán)境變量設(shè)置方法

    本文介紹了mysql數(shù)據(jù)庫(kù)中環(huán)境變量的設(shè)置方法,如何設(shè)置mysql數(shù)據(jù)庫(kù)的環(huán)境變量,有需要的朋友參考下
    2007-01-01
  • WordPress中用于檢索模版的相關(guān)PHP函數(shù)使用解析

    WordPress中用于檢索模版的相關(guān)PHP函數(shù)使用解析

    這篇文章主要介紹了WordPress中用于檢索模版的相關(guān)PHP函數(shù)使用解析,包括索模板的函數(shù)的使用,要的朋友可以參考下
    2015-12-12
  • PHP常用函數(shù)之base64圖片上傳功能詳解

    PHP常用函數(shù)之base64圖片上傳功能詳解

    這篇文章主要介紹了PHP常用函數(shù)之base64圖片上傳功能,結(jié)合實(shí)例形式分析了前臺(tái)ajax提交及后臺(tái)base64圖片編碼上傳相關(guān)操作技巧,需要的朋友可以參考下
    2019-10-10
  • 使用PHP 5.0創(chuàng)建圖形的巧妙方法

    使用PHP 5.0創(chuàng)建圖形的巧妙方法

    本文將展示如何使用 PHP 構(gòu)建面向?qū)ο?的)圖形層。使用面向?qū)ο?的)系統(tǒng)可以用來(lái)構(gòu)建復(fù)雜(的)圖形,這比使用標(biāo)準(zhǔn) PHP 庫(kù)中所提供(的)基本功能來(lái)構(gòu)建圖形簡(jiǎn)單(很)多。
    2010-10-10
  • PHP屏蔽蜘蛛訪問(wèn)代碼及常用搜索引擎的HTTP_USER_AGENT

    PHP屏蔽蜘蛛訪問(wèn)代碼及常用搜索引擎的HTTP_USER_AGENT

    屏蔽蜘蛛相信每一位站長(zhǎng)都不希望這樣做吧,因?yàn)橹┲氲脑L問(wèn)就沒(méi)有用戶的瀏覽,直接會(huì)給我們帶來(lái)一定損失,不過(guò)也有例外,某些網(wǎng)站就不希望被蜘蛛爬行,接下來(lái)為你介紹屏蔽蜘蛛的php代碼
    2013-03-03
  • PHP 七大優(yōu)勢(shì)分析

    PHP 七大優(yōu)勢(shì)分析

    ASP是微軟公司實(shí)現(xiàn)動(dòng)態(tài)網(wǎng)頁(yè)的一種技術(shù)。ASP支持一些腳本語(yǔ)言,主要以VBScripp為主。與ASP相比較,你還可以選擇另一中開(kāi)放源代碼編程語(yǔ)言——PHP,PHP可以運(yùn)行在多種操作系統(tǒng)下,其中包括Linux和windows。
    2009-06-06

最新評(píng)論