映月读书网 > 微信公众平台开发:从零基础到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 个性化欢迎语效果