<?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;
$customrevent = $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>";
switch ($type)
{ case "event";
if ($customrevent=="subscribe")
{$contentStr = "感谢你的关注\n回复1查看联系方式\n回复2查看最新资讯\n回复3查看法律文书";}
break;
case "image";
$contentStr = "你的图片很棒!";
break;
case "location";
$newsTpl ="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>4</ArticleCount>
<Articles>
<item>
<Title><![CDATA[你周边附近的酒店如下]]></Title>
<Description><![CDATA[]]></Description>
<PicUrl><![CDATA[http://weixin1234.duapp.com/2.jpg]]></PicUrl>
<Url><![CDATA[]]></Url>
</item>
<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[]]></Description>
<PicUrl><![CDATA[http://weixin1234.duapp.com/2.jpg]]></PicUrl>
<Url><![CDATA[]]></Url>
</item>
<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[]]></Description>
<PicUrl><![CDATA[http://weixin1234.duapp.com/2.jpg]]></PicUrl>
<Url><![CDATA[]]></Url>
</item>
<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[]]></Description>
<PicUrl><![CDATA[http://weixin1234.duapp.com/2.jpg]]></PicUrl>
<Url><![CDATA[]]></Url>
</item>
</Articles>
<FuncFlag>0</FuncFlag>
</xml>";
$hotel="http://api.map.baidu.com/telematics/v2/local?location={$longitude},{$latitude}&keyWord=酒店&ak=1a3cde429f38434f1811a75e1a90310c";
$apistr=file_get_contents($hotel);
$apiobj=simplexml_load_string($apistr);
$name1_obj=$apiobj->poiList->point[0]->name;//读取酒店名
$add1_obj=$apiobj->poiList->point[0]->address;//读取地址
$tel1_obj=$apiobj->poiList->point[0]->telephone;//读取电话
$price1_obj=$apiobj->poiList->point[0]->additionalInfo->price;//读取价格
$title1_str="{$name1}地址{$add1_obj}电话{$tel1_obj}价格{$price1_obj}元";
//构造图文标题
$name2_obj=$apiobj->poiList->point[1]->name;
$add2_obj=$apiobj->poiList->point[1]->address;
$tel2_obj=$apiobj->poiList->point[1]->telephone;
$price2_obj=$apiobj->poiList->point[1]->additionalInfo->price;
$title2_str="{$name2}地址{$add2_obj}电话{$tel2_obj}价格{$price2_obj}元";
//第二家酒店
$name3_obj=$apiobj->poiList->point[2]->name;
$add3_obj=$apiobj->poiList->point[2]->address;
$tel3_obj=$apiobj->poiList->point[2]->telephone;
$price3_obj=$apiobj->poiList->point[2]->additionalInfo->price;
$title3_str="{$name3}地址{$add3_obj}电话{$tel3_obj}价格{$price3_obj}元";
//第三家酒店
$resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time, $title1_str,$title2_str,$title3_str);//变量赋值给图文
echo $resultStr;
break;
case "link" ;
$contentStr = "你的链接有病毒吧!";
break;
case "text";
$weatherurl="http://api.map.baidu.com/telematics/v2/weather?location={$keyword}&ak=1a3cde429f38434f1811a75e1a90310c";
$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}";
break;
default;
$contentStr ="此项功能尚未开发";
}
$msgType="text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else {
echo "";
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;
}
}
}
?>
微信开发 PHP代码 周边酒店
猜你喜欢
转载自blog.csdn.net/qiphon3650/article/details/80716372
今日推荐
周排行