一、吹水
觉得网上学习资源好多,但是不会利用,真浪费!
*PPT设计(http://www.xiaojinke.com/ppt){五星}
http://book.douban.com/subject/10453373/ {想法再好也一定要表达出来,所以PPT制作技术非常重要}
*百度校园课堂(http://campus.baidu.com/experience)
*一些干货(http://www.liuxiaoyangzai.com/?p=69)
*微信框架(http://www.weiphp.cn/wiki/)
二、实际工作
百度地图API控制台(http://lbsyun.baidu.com/apiconsole/key?application=key)中的内容要仔细看。创建应用时候按照提示在“IP白名单”一栏填写0.0.0.0/0,获取ak密钥。
2.1 反Geocoding
反Geocoding是根据微信用户端发来的经纬度返回用户的地理位置信息。
由于我们微信公共平台面向的是很多的用户,我们不能固定解析一个经纬度,因此我们要构建一个动态的解析地址。在前面的开发中我们已经可以获取经纬度,分别赋予变量$latitude和$longitude。因此我们构建的URL为http://api.map.baidu.com/telematics/v2/reverseGeocoding?location={$longitude},{$latitude}&ak=8UGHSPObt5WTKQrotMFlXuSI(后面的ak为自己的ak)。我们通过函数file_get_contents函数和simple_load_string函数进行解析,我们要获取的地址进行逐级解析,第一级为results,第二级有n个result,第三级为name。因此,我们可以使用results->result[0]->name来获取地址。
修改SAE中版本5的代码,我们这次定位返回的信息是”我知道你在***附近“,代码如下:
<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
//$wechatObj->valid();
$wechatObj->responseMsg();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature())
{
echo $echoStr;
exit;
}
}
public function responseMsg()
{
//get post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//extract post data
if (!empty($postStr))
{
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$type =$postObj->MsgType;
$customevent=$postObj->Event;
$latitude=$postObj->Location_X;
$longitude=$postObj->Location_Y;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
$musicTpl="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Music>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<MusicUrl><![CDATA[%s]]></MusicUrl>
<HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
</Music>
</xml>";
$newsTpl="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>2</ArticleCount>
<Articles>
<item>
<Title><![CDATA[女神的气质大衣]]></Title>
<Description><![CDATA[一款舒适的长大衣是寒冷季节的首选]]></Description>
<PicUrl><![CDATA[http://4.zhangxuman.sinaapp.com/1.jpg]]></PicUrl>
<Url><![CDATA[http://www.baidu.com]]></Url>
</item>
<item>
<Title><![CDATA[墨概念呢子外套]]></Title>
<Description><![CDATA[秋冬女装大衣2014新款韩版修身大毛领毛呢外套]]></Description>
<PicUrl><![CDATA[http://4.zhangxuman.sinaapp.com/2.jpg]]></PicUrl>
<Url><![CDATA[http://www.baidu.com]]></Url>
</item>
</Articles>
</xml>";
if($type=="event" )//and $customrevent=="subscribe")
{
$msgType = "text";
$contentStr="感谢您的关注\n 回复1可查看联系方式 \n 回复2可还钱 \n 回复3可查看各种小吃";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
else
{
switch($type)
{
case "text":
{
if(!empty( $keyword ))
{
if($keyword=="1")
{
$msgType = "text";
$contentStr="广东工业大学工二馆,联系方式:18790652159,联系人:张星星";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
}
else if($keyword=="2")
{
$msgType = "text";
$contentStr="请将钱打入工行卡88888888,于五个工作日内,谢谢配合";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
}
else if($keyword=="3")
{
$msgType = "text";
$contentStr="饿了吗?河南小吃,广东小吃,台湾小吃,各种美味等着你哦";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
}
else if($keyword=="音乐")
{
$Title="纯真年代";
$Description="离婚前规则主题曲";
$MusicUrl="http://1.zhangxuman.sinaapp.com/music/1.mp3";
$HQMusicUrl= $MusicUrl;
$msgType="music";
$resultStr = sprintf($musicTpl, $fromUsername, $toUsername, $time, $msgType,$Title,$Description,$MusicUrl,$HQMusicUrl);
}
else if($keyword=="应用")
{
$msgType = "text";
$contentStr="\n<a href="http://haotianqi.duapp.com" >天气查询</a>\n\n\n<a href="http://h.lexun.com/game/DouDiZhu/play.aspx">
斗地主</a>\n\n\n<a href="http://jiemengmobi.duapp.com">周公解梦</a>\n\n\n<a href="http://pictoword.hortorgame.com">疯狂猜图</a>";
$resultStr = sprintf($textTpl2, $fromUsername, $toUsername, $time, $msgType, $contentStr);
}
else if($keyword=="购物时尚")
{
//$msgType="text";
$resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time);
}
else //这个else里面是调用图灵机的apidai'm
{
$apiKey = "70fc220d8d6364d9a6fe894d7ec7ec59";
$apiURL = "http://www.tuling123.com/openapi/api?key=KEY&info=INFO";//2 3 1
$url = str_replace("INFO", $keyword, str_replace("KEY", $apiKey, $apiURL));
/** 方法一、用file_get_contents 以get方式获取内容 */
$res =file_get_contents($url); //这三句是解析api返回的res,并用contentStr输出
$apiObj =json_decode($res);
$contentStr = $apiObj->text;
$msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
}
}
else
{
$contentStr= "Input something...";
}
break;
}
case "voice":
{
$msgType = "text";
$contentStr="语音功能尚未完善,请输入文本信息";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
break;
}
case "image":
{
$msgType = "text";
$contentStr="你的图片很漂亮";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
break;
}
case "location":
{
$geourl=; //反Geocoding接口
$apistr=file_get_contents($geourl); //读取文件
$apiobj=simplexml_load_string($apistr); //xml解析
$addstr=$apiobj->results->result[0]->name; //逐级解析
$contentStr="我知道你在{$addstr}附近";
$msgType = "text";
// $contentStr="你的纬度是{$latitude},经度是{$longitude},我们已经锁定!";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
break;
}
case "link":
{
$msgType = "text";
$contentStr="你的链接有病毒吧!";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
break;
}
default :
{
$msgType = "text";
$contentStr="此功能尚未开发";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
break;
}
}
}
echo $resultStr;
}
else
{
exit;
}
}
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;
}
}
}
?>
其实,也只是改变了case "location"里面的内容。
下面这段代码,其实也是改变了case"location"里面的代码,使得定位后回复“你的纬度是。。。经度是。。。,我们已经锁定!你离我的公司还有。。。公里远,我们(不)能送货上门。”
<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
//$wechatObj->valid();
$wechatObj->responseMsg();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature())
{
echo $echoStr;
exit;
}
}
public function responseMsg()
{
//get post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//extract post data
if (!empty($postStr))
{
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$type =$postObj->MsgType;
$customevent=$postObj->Event;
$latitude=$postObj->Location_X;
$longitude=$postObj->Location_Y;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
$musicTpl="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Music>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<MusicUrl><![CDATA[%s]]></MusicUrl>
<HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
</Music>
</xml>";
$newsTpl="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>2</ArticleCount>
<Articles>
<item>
<Title><![CDATA[女神的气质大衣]]></Title>
<Description><![CDATA[一款舒适的长大衣是寒冷季节的首选]]></Description>
<PicUrl><![CDATA[http://4.zhangxuman.sinaapp.com/1.jpg]]></PicUrl>
<Url><![CDATA[http://www.baidu.com]]></Url>
</item>
<item>
<Title><![CDATA[墨概念呢子外套]]></Title>
<Description><![CDATA[秋冬女装大衣2014新款韩版修身大毛领毛呢外套]]></Description>
<PicUrl><![CDATA[http://4.zhangxuman.sinaapp.com/2.jpg]]></PicUrl>
<Url><![CDATA[http://www.baidu.com]]></Url>
</item>
</Articles>
</xml>";
if($type=="event" )//and $customrevent=="subscribe")
{
$msgType = "text";
$contentStr="感谢您的关注\n 回复1可查看联系方式 \n 回复2可还钱 \n 回复3可查看各种小吃";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
else
{
switch($type)
{
case "text":
{
if(!empty( $keyword ))
{
if($keyword=="1")
{
$msgType = "text";
$contentStr="广东工业大学工二馆,联系方式:18790652159,联系人:张星星";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
}
else if($keyword=="2")
{
$msgType = "text";
$contentStr="请将钱打入工行卡88888888,于五个工作日内,谢谢配合";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
}
else if($keyword=="3")
{
$msgType = "text";
$contentStr="饿了吗?河南小吃,广东小吃,台湾小吃,各种美味等着你哦";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
}
else if($keyword=="音乐")
{
$Title="纯真年代";
$Description="离婚前规则主题曲";
$MusicUrl="http://1.zhangxuman.sinaapp.com/music/1.mp3";
$HQMusicUrl= $MusicUrl;
$msgType="music";
$resultStr = sprintf($musicTpl, $fromUsername, $toUsername, $time, $msgType,$Title,$Description,$MusicUrl,$HQMusicUrl);
}
else if($keyword=="应用")
{
$msgType = "text";
$contentStr="\n<a href="http://haotianqi.duapp.com" >天气查询</a>\n\n\n<a href="http://h.lexun.com/game/DouDiZhu/play.aspx">
斗地主</a>\n\n\n<a href="http://jiemengmobi.duapp.com">周公解梦</a>\n\n\n<a href="http://pictoword.hortorgame.com">疯狂猜图</a>";
$resultStr = sprintf($textTpl2, $fromUsername, $toUsername, $time, $msgType, $contentStr);
}
else if($keyword=="购物时尚")
{
//$msgType="text";
$resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time);
}
else //这个else里面是调用图灵机的apidai'm
{
$apiKey = "70fc220d8d6364d9a6fe894d7ec7ec59";
$apiURL = "http://www.tuling123.com/openapi/api?key=KEY&info=INFO";//2 3 1
$url = str_replace("INFO", $keyword, str_replace("KEY", $apiKey, $apiURL));
/** 方法一、用file_get_contents 以get方式获取内容 */
$res =file_get_contents($url); //这三句是解析api返回的res,并用contentStr输出
$apiObj =json_decode($res);
$contentStr = $apiObj->text;
$msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
}
}
else
{
$contentStr= "Input something...";
}
break;
}
case "voice":
{
$msgType = "text";
$contentStr="语音功能尚未完善,请输入文本信息";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
break;
}
case "image":
{
$msgType = "text";
$contentStr="你的图片很漂亮";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
break;
}
case "location":
{
$geourl="http://api.map.baidu.com/telematics/v2/distance?waypoints=23.037914,113.398453;{$longitude},{$latitude}&ak=8UGHSPObt5WTKQrotMFlXuSI"; //测距api
$apistr=file_get_contents($geourl);
$apiobj=simplexml_load_string($apistr);
$distanceobj=$apiobj->results->distance;
$distanceint=intval($distanceobj); //转换为整数
$diskmint=$distanceint/1000; //转换为公里
if($diskmint<1000)
{$contentStr="你的纬度是{$latitude},经度是{$longitude},我们已经锁定!你离我的公司还有{$diskmint}公里远,我们可以免费送货上门";}
else{$contentStr="你的纬度是{$latitude},经度是{$longitude},我们已经锁定!你离我的公司还有{$diskmint}公里远,我们不能送货上门";}
//$contentStr="你离我的公司和还有{$distanceobj}米远";
$msgType = "text";
// $contentStr="你的纬度是{$latitude},经度是{$longitude},我们已经锁定!";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
break;
}
case "link":
{
$msgType = "text";
$contentStr="你的链接有病毒吧!";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
break;
}
default :
{
$msgType = "text";
$contentStr="此功能尚未开发";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
break;
}
}
}
echo $resultStr;
}
else
{
exit;
}
}
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;
}
}
}
?>
下面的代码是寻找附近三个店铺离你的地址最近的店铺,回复是“离你最近的店铺有。。米远,位置是N号路”
case "location":
{
$geourl1="http://api.map.baidu.com/telematics/v2/distance?waypoints=23.037914,113.398453;{$longitude},{$latitude}&ak=8UGHSPObt5WTKQrotMFlXuSI"; //测距api
$apistr1=file_get_contents($geourl1);
$apiobj1=simplexml_load_string($apistr1);
$distanceobj1=$apiobj1->results->distance;
$distanceint1=intval($distanceobj1); //转换为整数
$geourl2="http://api.map.baidu.com/telematics/v2/distance?waypoints=23.037914,113.398453;{$longitude},{$latitude}&ak=8UGHSPObt5WTKQrotMFlXuSI"; //测距api
$apistr2=file_get_contents($geourl2);
$apiobj2=simplexml_load_string($apistr2);
$distanceobj2=$apiobj2->results->distance;
$distanceint2=intval($distanceobj2); //转换为整数
$geourl3="http://api.map.baidu.com/telematics/v2/distance?waypoints=23.037914,113.398453;{$longitude},{$latitude}&ak=8UGHSPObt5WTKQrotMFlXuSI"; //测距api
$apistr3=file_get_contents($geourl3);
$apiobj3=simplexml_load_string($apistr3);
$distanceobj3=$apiobj3->results->distance;
$distanceint3=intval($distanceobj3); //转换为整数
$mindistanceint=min($distanceint1,$distanceint2,$distanceint3); //最近的店铺距离
if($mindistanceint==$distanceint1){$contentStr="离你最近的店铺有{$mindistanceint}米远,位置是1号路";}
else if($mindistanceint==$distanceint2){$contentStr="离你最近的店铺有{$mindistanceint}米远,位置是2号路";}
else{$contentStr="离你最近的店铺有{$mindistanceint}米远,位置是3号路";}
、 $msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
break;
}
由于前面都一样,这里就只写了case"location"里面的部分。
下面的代码是实现输入城市的名称,返回城市的日期天气,如输入“广州”,则回复“广州周五11月07日(实时:17.C)天气小雨转小到中雨,风力微风,温度22~16.C”。即实现了天气查询。(在我的SAE第7个版本)
switch($type)
{
case "text":
{
$weatherurl="http://api.map.baidu.com/telematics/v2/weather?location={$keyword}&ak=8UGHSPObt5WTKQrotMFlXuSI"; //天气预报接口
$apistr=file_get_contents($weatherurl);
$apiobj=simplexml_load_string($apistr);
$placeobj=$apiobj->currentCity; //读取城市
$todayobj=$apiobj->results->result[0]->date; //读取星期
$weatherobj=$apiobj->results->result[0]->weather; //读取天气
$windobj=$apiobj->results->result[0]->wind; //读取风力
$temobj=$apiobj->results->result[0]->temperature; //读取温度
$contentStr="{$placeobj}{$todayobj}天气{$weatherobj},风力{$windobj},温度{$temobj}";
$msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
break;
}
还有两个功能没有实现,一个就是版本6的定位查天气,但是不知道怎么回事出不来预想的结果,代码如下
case "location":
{
$weatherurl="http://api.map.baidu.com/telematics/v2/weather?location={$longitude},{latitude}&ak=8UGHSPObt5WTKQrotMFlXuSI"; //天气预报接口
$apistr=file_get_contents($weatherurl);
$apiobj=simplexml_load_string($apistr);
$placeobj=$apiobj->currentCity;
$todayobj=$apiobj->results->result[0]->date;
$weatherobj=$apiobj->results->result[0]->weather;
$windobj=$apiobj->results->result[0]->wind;
$temobj=$apiobj->results->result[0]->temperature;
$contentStr="{$placeobj}{$todayobj}天气{$weatherobj},风力{$windobj},温度{$temobj}";
$msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
break;
}
还有一个就是输入“应用”,回复四个简单的应用,但是开始时候可以,后来改了一下网址发现不行,又改回原来的,结果还是不行,我想是格式空格的问题吧!这段代码是在case"text"里面的一个else if分支里,代码如下(版本6)
else if($keyword=="应用")
{
$msgType = "text";
$contentStr="\n<a href="http://haotianqi.duapp.com" >天气查询</a>\n\n\n<a href="http://h.lexun.com/game/DouDiZhu/play.aspx">
斗地主</a>\n\n\n<a href="http://jiemengmobi.duapp.com">周公解梦</a>\n\n\n<a href="http://pictoword.hortorgame.com">疯狂猜图</a>";
$resultStr = sprintf($textTpl2, $fromUsername, $toUsername, $time, $msgType, $contentStr);
}
微信就先搞到这里了,有时间了再继续。那本《微信公共平台搭建与开发揭秘》易伟 真的不错,写的很详细,只是内容有些老了,不过还可以学习,再加上C大神和F大神的帮助,我学了一个星期学习了这么多,再次感谢C和F!!!