小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

獲取access_token - 傻瓜式微信開發(fā)教程7 - 耗子原創(chuàng)

 取經(jīng)悟能 2015-10-10
本帖最后由 moremorefun 于 2015-8-18 14:39 編輯

現(xiàn)在我們嘗試首次調(diào)用微信提供給我們的API.


微信的API大部分是需要`access_token`作為驗證字段了,
那我們首先嘗試獲取`access_token`.


我們這次帖子的主要目的是在用戶發(fā)送給我們的公眾號一個文本消息的時候返回給用戶我們獲取到的access_token.


根據(jù)我們在[回復(fù)簡單的文本消息 - 傻瓜式微信開發(fā)教程4 - 耗子原創(chuàng)]中的說明,
我們對用戶的文本消息在`index.php`頁面中的`onText()`函數(shù)中進(jìn)行處理.
微信關(guān)于獲取`access token`的說明在這里: http://mp.weixin.qq.com/wiki/11/0e4b294685f817b95cbed85ba5e82b8f.html


在說明中我們可以看到,獲取`access_token`需要提供`appid`和`secret`兩個參數(shù),
而之前我們的Wechat-php庫中沒有寫入secret參數(shù),所以我們還要對`Wechat.php`做一些修改,
主要是為了保存`appid`和`secret`兩個字段.


所以我們修改的`Wechat.php`的代碼為:

  1. <?php
  2.   class Wechat {
  3.     // ....
  4.     // ....
  5.     // ....
  6.     protected $encrypted = false;
  7.     protected $appid = '';
  8.     protected $appsecret = '';

  9.     // 添加appsecret參數(shù)
  10.     public function __construct($config=array('token'=>'', 'aeskey'=>'', 'appid'=>'', 'appsecret'=>'', 'debug' => FALSE)) {

  11.       $token = $config['token'];
  12.       $aeskey = $config['aeskey'];
  13.       $appid = $config['appid'];
  14.       $debug = $config['debug'];
  15.       // 將兩個參數(shù)儲存在實例中
  16.       $this->appid = $config['appid'];
  17.       $this->appsecret = $config['appsecret'];

  18.       // ...
  19.       // ...
  20.       // ...
  21.     }
  22.   }
復(fù)制代碼

我們的調(diào)用函數(shù)為:

  1. <?php
  2. /**
  3. * 微信開發(fā)者社區(qū): http:// 原創(chuàng)首發(fā)
  4. *
  5. * 微信公眾平臺 PHP SDK 示例文件
  6. */

  7.   require('wechat/Wechat.php');

  8.   /**
  9.    * 微信公眾平臺演示類
  10.    */
  11.   class TestWechat extends Wechat {
  12.     /**
  13.      * 收到文本消息時觸發(fā),回復(fù)收到的文本消息內(nèi)容
  14.      *
  15.      * @return void
  16.      */
  17.     protected function onText() {
  18.       // 獲取到 appid 和 appsecret
  19.       $appid = $this->appid;
  20.       $appsecret = $this->appsecret;
  21.       // 構(gòu)建獲取access_token的url
  22.       $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
  23.       // 構(gòu)建http請求并執(zhí)行
  24.       $ch = curl_init();
  25.       curl_setopt($ch, CURLOPT_URL, $url);
  26.       curl_setopt($ch, CURLOPT_HEADER, false);
  27.       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  28.       $result=curl_exec($ch);
  29.       curl_close($ch);
  30.       // 解析返回的json數(shù)據(jù)
  31.       $jsoninfo = json_decode($result);
  32.       // 讀取json中的access_token字段
  33.       $access_token = $jsoninfo->access_token;
  34.       $expires_in = $jsoninfo->expires_in;
  35.       // 將獲取到的access_token作為文本信息返回
  36.       $this->responseText("access_token: '{$access_token}'\nexpires_in: '{$expires_in}'");
  37.     }
  38.   }

  39.   // 這里調(diào)用新的
  40.   $wechat = new TestWechat(array(
  41.     'token'     => 'xxxx',              // 更新為自己的
  42.     'aeskey'    => 'xxxx',             // 更新為自己的
  43.     'appid'     => 'xxxx',              // 更新為自己的
  44.     'appsecret' => 'xxxx',          // 更新為自己的
  45.     'debug'     => true
  46.     ));
  47.   $wechat->run();
復(fù)制代碼

附件中有整個代碼的壓縮包
游客,如果您要查看本帖隱藏內(nèi)容請回復(fù)


    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多