文档介绍:微信公众平台开发笔记
这次我分享的笔记主要是一些细小的功能点的实现。
首先我画了一个图解释了我做的思路,如何处理收到text文本消息。
-----------------------------------------------------------------------------------------------------------------------------------------------------------
针对这里可能使用缓存会比较好,但是百度云账号的缓存要收费。
------------------------------------------------------------------------------------------------------------------------------------------------------------------
首先我们接着上面用户刚订阅你们消息的时候返回消息的处理,效果如下
 
private function receiveEvent($object)
{
$contentStr = "";
switch($object -> Event)
{
//注意这里是订阅消息微信给你提供的定义的事件
case "subscribe":
//关注後自动推送消息
$contentStr = '感谢您的关注/::)
快递查询请#加上快递名称前两个汉字,加单号,如#申通123456789
图书馆查询,请#加书名,如#node开发指南
天气查询,点下面加符号,发送地址位置即可。';
break;
}
//返回消息到模板
$resultStr = $this -> transmitText($object, $contentStr);
return $resultStr;
}
 
---------------------------------------------------------------------------------------------------------------------------------------------------------------
第二我接着我上次那个用户发送地理位置信息然后返回天气的处理。
private function receiveLocation($object)
{
$funcFlag = 0;
//获取经纬度,详情可以参考百度地图APi
$latitude = $object->Location_X;
$longitude = $object->Location_Y;
$contentStr = "";
$resultStr = "";
 
$weatherUrl = "./telematics/v2/weather?location={$longitude},{$latitude}&output=xml&354df507bfd5fe907241cfb68e";
//下面的数据就是根据XML数据的格式来进行判断和获取
$weatherApiStr = file_get_contents($weatherUrl);
$weatherApiObj = simplexml_load_string($weatherApiStr);
$placeObj = $weatherApiObj->currentCity; //读取城市
$todaydata = $weatherApiObj->results->result[0]->data;
$todayweather = $weatherApiObj->results->result[0]->weather;
$todaywind = $weatherApiObj->results->result[0]->wind;
$todaytemperature = $weatherApiObj->results->result[0]->temperature;
//数据以用形参的方式传给模板进行封装。
$contentStr = "{$placeObj}{$todaydata}天气{$todayweather},风力{$todaywind},温度{$todaytemperature}";
$resultStr = $this -> transmitText($object, $contentStr);
return $resultStr;