讀古今文學網 > 微信公眾平台開發:從零基礎到ThinkPHP5高性能框架實踐 > 6.6 案例實踐 >

6.6 案例實踐

6.6.1 個性化歡迎語

很多公眾號在用戶關注的時候,會顯示出用戶的暱稱、頭像或其他信息。下面以關注的時候向用戶提供當地天氣預報為例,介紹如何開發個性化歡迎語的功能。

用戶關注時會向開發者接口上報關注事件,我們提取用戶的OpenID,然後根據OpenID查詢用戶的基本信息,再根據用戶基本信息中的城市名稱查詢該城市的天氣預報。

獲取天氣預報的方法在第4章已介紹,這裡就不再重複。

獲取用戶基本信息的程序封裝到SDK的代碼如下。


 1 class class_weixin
 2 {
 3     var $appid = APPID;
 4     var $appsecret = APPSECRET;
 5 
 6     // 構造函數,獲取Access Token
 7     public function __construct($appid = NULL, $appsecret = NULL)
 8     {
 9         if($appid && $appsecret){
10             $this->appid = $appid;
11             $this->appsecret = $appsecret;
12         }
13         $url = "https:// api.weixin.qq.com/cgi-bin/token?grant_type=client_creden
           tial&appid=".$this->appid."&secret=".$this->appsecret;
14         $res = $this->http_request($url);
15         $result = json_decode($res, true);
16         $this->access_token = $result["access_token"];
17         $this->expires_time = time;
18     }
19 
20     // 獲取用戶基本信息
21     public function get_user_info($openid)
22     {
23         $url = "https:// api.weixin.qq.com/cgi-bin/user/info?access_token=".$this->
           access_token."&openid=".$openid."&lang=zh_CN";
24         $res = $this->http_request($url);
25         return json_decode($res, true);
26     }
27 
28     // HTTP請求(支持HTTP/HTTPS,支持GET/POST)
29     protected function http_request($url, $data = null)
30     {
31         $curl = curl_init;
32         curl_setopt($curl, CURLOPT_URL, $url);
33         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
34         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
35         if (!empty($data)){
36             curl_setopt($curl, CURLOPT_POST, 1);
37             curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
38         }
39         curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
40         $output = curl_exec($curl);
41         curl_close($curl);
42         return $output;
43     }
44 }
  

在接收消息中,處理流程如下。


 1 // 接收事件消息
 2 private function receiveEvent($object)
 3 {    
 4     $openid = strval($object->FromUserName);
 5     $content = "";
 6     switch ($object->Event)
 7     {
 8         case "subscribe":
 9             require_once('weixin.class.php');
10             $weixin = new class_weixin;
11             $info = $weixin->get_user_info($openid);
12             $municipalities = array("北京", "上海", "天津", "重慶", "香港", "
               澳門");
13             if (in_array($info['province'], $municipalities)){
14                 $info['city'] = $info['province'];
15             }
16             $content = "歡迎關注,".$info['nickname'];
17             if ($info['country'] == "中國"){
18                 require_once('weather.php');
19                 $weatherInfo = getWeatherInfo($info['city']);
20                 $content .= "\r\n您來自 ".$info['city'].",當前天氣如下\r\n".$weather
                   Info[1]["Title"];
21             }
22             break;
23         default:
24             $content = "receive a new event: ".$object->Event;
25             break;
26     }
27     if(is_array($content)){
28         $result = $this->transmitNews($object, $content);
29     }else{
30         $result = $this->transmitText($object, $content);
31     }
32     return $result;
33 }
  

上述代碼解讀如下。

第4行:提取用戶的OpenID。

第5行:初始化回復內容。

第6~8行:檢測當前事件是否為關注事件,並進入相應的事件處理流程。

第9~11行:包含類文件;聲明一個新的類對像;查詢用戶基本信息。

第12~15行:這裡是一個數據處理流程,當用戶在「北京」、「上海」、「天津」、「重慶」、「香港」、「澳門」等城市的時候,用戶信息中省名為城市名,市名為區名。例如,一個在北京石景山區的用戶,他的基本信息中省名為「北京」,市名為「石景山」。這裡為了統一,將用戶的省名設置為市名。

第16行:將用戶的暱稱放入歡迎語中。

第17~21行:如果用戶信息中國名為「中國」,則查詢其所在城市的天氣預報信息並添加到歡迎語中。

第27~32行:自動判斷當前$content是否為數組,並調用不同的格式回復給用戶。

一個查詢成功的結果如圖6-1所示。

圖6-1 個性化歡迎語效果