微信公共平台开发原理和步骤:
1. 搭建自己的服务器和域名绑定,并可以通过网络访问到该服务器
2. 申请称为微信公共平台开发者
3. 申请通过后开发自己的微信产品
原理:用户通过客户端发送的数据通过微信服务器的再包装,然后post到自己的服务器,自己的服务器处理这些数据后包装成xml格式的数据在post到微信服务器,这里微信服务器其实就是包装和中转了一下,然后返回给用户
案例:
<?php
//TOKEN 为申请微信开发者时自己制定的名字
define("TOKEN", "zangbc");
$wechatObj = new wechatCallbackapiTest();
if (isset($_GET['echostr'])) {
$wechatObj->valid();
}else{
$wechatObj->responseMsg();
}
class wechatCallbackapiTest
{
//如果合法则返回签名串给微信服务器
public function valid()
{
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
//通过一个签名串来检测该开发者是否合法
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
//通过用户输入的关键字来判断返回相应的数据
public function responseMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
if(!empty( $keyword ))
{
if( $keyword=="星座")
{
$textTpl=$this->setResponseXml("星座查询方式:
回复: 生日日日期 如 【1999.1.1】");
$msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType);
echo $resultStr;
}else if(count(explode('.',$keyword))==3){
$keyword1 = explode('.',$keyword);
$textTpl=$this->getTextTpl_XinZuo($keyword1);
$msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType);
echo $resultStr;
}else if(mb_substr($keyword,2,2,"utf-8")=="天气"){
$textTpl=$this->getTextTpl_Weather($keyword);
//$msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time);
echo $resultStr;
}else if($keyword=="野树林"){
$textTpl=$this->setResponseXml("#野树林工作室#
【1】星座查询,回复【星座】
【2】天气查询,回复【城市名+天气】如 杨凌天气
【3】歌曲欣赏,回复【歌曲+歌曲名】如 歌曲时间都去哪儿了");
$msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType);
echo $resultStr;
}else if(mb_substr($keyword,0,2,"utf-8")=="歌曲"){
//$textTpl=$this->setResponseXml();
$textTpl=$this->getTextTpl_Songs($keyword);
$msgType = "music";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType);
echo $resultStr;
}else{
$textTpl=$this->setResponseXml("我还在成长中哦~
更多功能请回复【野树林】");
$msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType);
echo $resultStr;
}
}else{
echo "Input something...";
}
}else {
echo "";
exit;
}
}
//返回格式为音乐的xml
private function setResponseXmlMusic($songObj)
{
if(@$songObj['music']!=null)
{
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Music>
<Title><![CDATA[".$songObj['music']['title']."]]></Title>
<Description><![CDATA[]]></Description>
<MusicUrl><![CDATA[".$songObj['music']['musicurl']."]]></MusicUrl>
<HQMusicUrl><![CDATA[".$songObj['music']['hqmusicurl']."]]></HQMusicUrl>
</Music>
<FuncFlag>0</FuncFlag>
</xml>";
}else{
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Music>
<Title><![CDATA[时间都去哪儿了]]></Title>
<Description><![CDATA[]]></Description>
<MusicUrl><![CDATA[http://play.baidu.com/?__m=mboxCtrl.playSong&__a=14385500&__o=/song/14385500||playBtn&fr=altg3||www.baidu.com#loaded]]></MusicUrl>
<HQMusicUrl><![CDATA[http://play.baidu.com/?__m=mboxCtrl.playSong&__a=14385500&__o=/song/14385500||playBtn&fr=altg3||www.baidu.com#loaded]]></HQMusicUrl>
</Music>
<FuncFlag>0</FuncFlag>
</xml>";
}
return $textTpl;
}
//用来获取网络中的音乐资源
private function getTextTpl_Songs($keyword)
{
$songname=mb_substr($keyword,2,strlen($keyword),"utf-8");
$url="http://api2.sinaapp.com/search/music/?appkey=0020130430&appsecert=fa6095e1133d28ad&reqtype=music&keyword=".urlencode($songname);
$song=file_get_contents($url);
$songObj = json_decode($song, true);//转为json格式数据
$textTpl=$this->setResponseXmlMusic($songObj);
return $textTpl;
}
//返回从自己服务器端文件中获取到的星座信息
private function getXinZuo($filepath)
{
return file_get_contents($filepath);
}
//获取12个星座所对应的xml格式数据
private function getTextTpl_XinZuo($keyword)
{
if(count($keyword)==3)
{
switch($keyword[1])
{
case 1:
if($keyword[2]>=1&&$keyword[2]<=19)
{
$textTpl=$this->setResponseXml($this->getXinZuo("XinZuo/MoXie.txt"));
}else if($keyword[2]>=20&&$keyword[2]<=31)
{
$textTpl=$this->setResponseXml($this->getXinZuo("XinZuo/ShuiPing.txt"));
}
break;
case 2:
if($keyword[2]>=1&&$keyword[2]<=18)
{
$textTpl=$this->setResponseXml($this->getXinZuo("XinZuo/ShuiPing.txt"));
}else if($keyword[1]>=19&&$keyword[1]<=28)
{
$textTpl=$this->setResponseXml($this->getXinZuo("XinZuo/ShuangYu.txt"));
}
break;
case 3:
if($keyword[2]>=1&&$keyword[2]<=20)
{
$textTpl=$this->setResponseXml($this->getXinZuo("XinZuo/ShuangYu.txt"));
}else if($keyword[1]>=21&&$keyword[1]<=31)
{
$textTpl=$this->setResponseXml($this->getXinZuo("XinZuo/BaiYang.txt"));
}
break;
case 4:
if($keyword[2]>=1&&$keyword[2]<=19)
{
$textTpl=$this->setResponseXml($this->getXinZuo("XinZuo/BaiYang.txt"));
}else if($keyword[1]>=20&&$keyword[1]<=30)
{
$textTpl=$this->setResponseXml($this->getXinZuo("XinZuo/JinNiu.txt"));
}
break;
case 5:
if($keyword[2]>=1&&$keyword[2]<=20)
{
$textTpl=$this->setResponseXml($this->getXinZuo("XinZuo/JinNiu.txt"));
}else if($keyword[1]>=21&&$keyword[1]<=31)
{
$textTpl=$this->setResponseXml($this->getXinZuo("XinZuo/ShuangZi.txt"));
}
break;
case 6:
if($keyword[2]>=1&&$keyword[2]<=21)
{
$textTpl=$this->setResponseXml($this->getXinZuo("XinZuo/ShuangZi.txt"));
}else if($keyword[2]>=22&&$keyword[2]<=30)
{
$textTpl=$this->setResponseXml($this->getXinZuo("XinZuo/JuXie.txt"));
}
break;
case 7:
if($keyword[2]>=1&&$keyword[2]<=22)
{
$textTpl=$this->setResponseXml($this->getXinZuo("XinZuo/JuXie.txt"));
}else if($keyword[1]>=23&&$keyword[1]<=31)
{
$textTpl=$this->setResponseXml($this->getXinZuo("XinZuo/ShiZi.txt"));
}
break;
case 8:
if($keyword[2]>=1&&$keyword[2]<=22)
{
$textTpl=$this->setResponseXml($this->getXinZuo("XinZuo/ShiZi.txt"));
}else if($keyword[1]>=23&&$keyword[1]<=31)
{
$textTpl=$this->setResponseXml($this->getXinZuo("XinZuo/ChuNv.txt"));
}
break;
case 9:
if($keyword[2]>=1&&$keyword[2]<=22)
{
$textTpl=$this->setResponseXml($this->getXinZuo("XinZuo/ChuNv.txt"));
}else if($keyword[1]>=23&&$keyword[1]<=30)
{
$textTpl=$this->setResponseXml($this->getXinZuo("XinZuo/TianPing.txt"));
}
break;
case 10:
if($keyword[2]>=1&&$keyword[2]<=23)
{
$textTpl=$this->setResponseXml($this->getXinZuo("XinZuo/TianPing.txt"));
}else if($keyword[1]>=24&&$keyword[1]<=31)
{
$textTpl=$this->setResponseXml($this->getXinZuo("XinZuo/TianXie.txt"));
}
break;
case 11:
if($keyword[2]>=1&&$keyword[2]<=22)
{
$textTpl=$this->setResponseXml($this->getXinZuo("XinZuo/TianXie.txt"));
}else if($keyword[1]>=23&&$keyword[1]<=30)
{
$textTpl=$this->setResponseXml($this->getXinZuo("XinZuo/SheShou.txt"));
}
break;
case 12:
if($keyword[2]>=1&&$keyword[2]<=21)
{
$textTpl=$this->setResponseXml($this->getXinZuo("XinZuo/SheShou.txt"));
}else if($keyword[1]>=22&&$keyword[1]<=31)
{
$textTpl=$this->setResponseXml($this->getXinZuo("XinZuo/MoXie.txt"));
}
break;
default:
$textTpl="~~o(>_<)o ~~
亲,你输入的生日有误幺!
正确输入格式如下[2014.1.1]";
break;
}
}else{
$textTpl=$this->setResponseXml("~~o(>_<)o ~~
亲,你输入的生日有误幺!
正确输入格式如下[2014.1.1]");
}
return $textTpl;
}
//获取网络中的天气资源
private function getTextTpl_Weather($keyword)
{
$cityname=trim(mb_substr($keyword,0,2,"utf-8"));
$url = "http://api.map.baidu.com/telematics/v3/weather?location=".urlencode($cityname)."&output=json&ak=5slgyqGDENN7Sy7pw29IUvrZ";
$weatherJson = file_get_contents($url);
$weather = json_decode($weatherJson, true);
$textTpl=$this->setResponseXmlWeather($weather);
return $textTpl;
}
//设置天气返回的图文格式xml数据
private function setResponseXmlWeather($weather)
{
if(@$weather['results']!=null)
{
$textTpl="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<Content><![CDATA[]]></Content>
<ArticleCount>4</ArticleCount>
<Articles>
<item>
<Title><![CDATA[【".$weather['results'][0]['currentCity']."】天气预报 ".$weather['results'][0]['weather_data'][0]['date']." ".$weather['results'][0]['weather_data'][0]['weather']." ".$weather['results'][0]['weather_data'][0]['wind']." ".$weather['results'][0]['weather_data'][0]['temperature']."]]></Title>
<Description><![CDATA[白天]]></Description>
<PicUrl><![CDATA[http://t11.baidu.com/it/u=3787200454,2899262770&fm=90&gp=0.jpg]]></PicUrl>
<Url><![CDATA[]]></Url>
</item>
<item>
<Title><![CDATA[".$weather['results'][0]['weather_data'][1]['date']." ".$weather['results'][0]['weather_data'][1]['weather']." ".$weather['results'][0]['weather_data'][1]['wind']." ".$weather['results'][0]['weather_data'][1]['temperature']."]]></Title>
<Description><![CDATA[白天]]></Description>
<PicUrl><![CDATA[".$weather['results'][0]['weather_data'][1]['dayPictureUrl']."]]></PicUrl>
<Url><![CDATA[]]></Url>
</item>
<item>
<Title><![CDATA[".$weather['results'][0]['weather_data'][2]['date']." ".$weather['results'][0]['weather_data'][2]['weather']." ".$weather['results'][0]['weather_data'][2]['wind']." ".$weather['results'][0]['weather_data'][2]['temperature']."]]></Title>
<Description><![CDATA[白天]]></Description>
<PicUrl><![CDATA[".$weather['results'][0]['weather_data'][2]['dayPictureUrl']."]]></PicUrl>
<Url><![CDATA[]]></Url>
</item>
<item>
<Title><![CDATA[".$weather['results'][0]['weather_data'][3]['date']." ".$weather['results'][0]['weather_data'][3]['weather']." ".$weather['results'][0]['weather_data'][3]['wind']." ".$weather['results'][0]['weather_data'][3]['temperature']."]]></Title>
<Description><![CDATA[白天]]></Description>
<PicUrl><![CDATA[".$weather['results'][0]['weather_data'][3]['dayPictureUrl']."]]></PicUrl>
<Url><![CDATA[]]></Url>
</item>
</Articles>
<FuncFlag>0</FuncFlag>
</xml>";
}else{
$textTpl="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[~~o(>_<)o ~~抱歉,暂无次城市天气实况]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
}
return $textTpl;
}
//设置普通文本的xml格式数据
private function setResponseXml($stringXml)
{
$stringXml="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[".$stringXml."]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
return $stringXml;
}
}
?>