php微信公眾開發(fā)之獲取周邊酒店信息的方法
更新時間:2014年12月22日 15:35:07 投稿:shichen2014
這篇文章主要介紹了php微信公眾開發(fā)之獲取周邊酒店信息的方法,涉及針對微信公眾平臺關于地理信息的應用技巧,具有一定的參考借鑒價值,需要的朋友可以參考下
本文實例講述了php微信公眾開發(fā)之獲取周邊酒店信息的方法。分享給大家供大家參考。具體分析如下:
關注微信公眾之后發(fā)送回復地理位置信息,即可回復周邊附近的酒店信息列表,下面我就來給各位介紹利用php是怎么實現(xiàn)這個功能,希望全子對大家有幫助.代碼如下:
復制代碼 代碼如下:
<?php
//將提交過來的信息接收
$signature = $_GET['signature'];
$timestamp = $_GET['timestamp'];
$nonce = $_GET['nonce'];
$echostr = $_GET['echostr'];
$token = "jb51.net";
//判斷接入網(wǎng)站
//進行字典排序
$arr = array($token,$timestamp,$nonce);
sort($arr);
//完成字符串的拼接和sha1加密
$result = sha1(join($arr));
//判斷生成的字符串和$signature是否相等,如果相等,直接輸出$echostr,這樣網(wǎng)站接入成功
if($result==$signature){
echo $echostr;
}
//接收微信公眾賬號接收到的信息
$poststr = $GLOBALS["HTTP_RAW_POST_DATA"];
$xmlObj = simplexml_load_string($poststr,'SimpleXMLElement',LIBXML_NOCDATA);
$ToUserName = $xmlObj->ToUserName;
$FromUserName = $xmlObj->FromUserName;
$CreateTime = $xmlObj->CreateTime;
$MsgType = $xmlObj->MsgType;
$Content = $xmlObj->Content;
if($MsgType=='location'){
$Location_X = $xmlObj->Location_X;
$Location_Y = $xmlObj->Location_Y;
$Scale = $xmlObj->Scale;
$Label = $xmlObj->Label;
$urlstr = "http://api.map.baidu.com/place/v2/search?&query=酒店&location=".$Location_X.",".$Location_Y."&radius=5000&output=json&ak=DESY8unmZnUlLB0mlowjuiRr"; //此處ak參數(shù)需要個人的百度開發(fā)序列號,自己去百度申請下就好了
$jsonstr = file_get_contents($urlstr);
$json = json_decode($jsonstr,true);
$pic_640 = "http://api.map.baidu.com/staticimage?width=640&height=320¢er=".$Location_Y.",".$Location_X."&zoom=15&markers=".$Location_Y.",".$Location_X."&markerStyles=l,";
$pic_80 = "http://api.map.baidu.com/staticimage?width=80&height=80¢er=".$Location_Y.",".$Location_X."&zoom=15&markers=".$Location_Y.",".$Location_X."&markerStyles=l,";
$p_640 = file_get_contents($pic_640);
file_put_contents('./images/640_'.$FromUserName.".png",$p_640);
$p_80 = file_get_contents($pic_80);
file_put_contents('./images/80_'.$FromUserName.".png",$p_80);
echo pic_send($json['results']);
}
function pic_send($arr){
global $ToUserName,$FromUserName;
$str = "<xml>
<ToUserName><![CDATA[".$FromUserName."]]></ToUserName>
<FromUserName><![CDATA[".$ToUserName."]]></FromUserName>
<CreateTime>".time()."</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>".count($arr)."</ArticleCount>
<Articles>";
foreach($arr as $k=>$v){
if($k==0){
$picurl = "http://jb51.net/weixin/images/640_".$FromUserName.".png";
}else{
$picurl = "http://jb51.net.net/weixin/images/80_".$FromUserName.".png";
}
$str .="
<item>
<Title><![CDATA[".$v['name']." 地址:".$v['address']." 電話:".$v['telephone']."]]></Title>
<Description><![CDATA[".$v['name']." 地址:".$v['address']." 電話:".$v['telephone']."]]></Description>
<PicUrl><![CDATA[".$picurl."]]></PicUrl>
<Url><![CDATA[http://api.map.baidu.com/place/detail?uid=".$v['uid']."&output=html&src=".$v['name']."&output=html]]></Url>
</item>";
}
$str .= "</Articles></xml>";
return $str;
}
?>
//將提交過來的信息接收
$signature = $_GET['signature'];
$timestamp = $_GET['timestamp'];
$nonce = $_GET['nonce'];
$echostr = $_GET['echostr'];
$token = "jb51.net";
//判斷接入網(wǎng)站
//進行字典排序
$arr = array($token,$timestamp,$nonce);
sort($arr);
//完成字符串的拼接和sha1加密
$result = sha1(join($arr));
//判斷生成的字符串和$signature是否相等,如果相等,直接輸出$echostr,這樣網(wǎng)站接入成功
if($result==$signature){
echo $echostr;
}
//接收微信公眾賬號接收到的信息
$poststr = $GLOBALS["HTTP_RAW_POST_DATA"];
$xmlObj = simplexml_load_string($poststr,'SimpleXMLElement',LIBXML_NOCDATA);
$ToUserName = $xmlObj->ToUserName;
$FromUserName = $xmlObj->FromUserName;
$CreateTime = $xmlObj->CreateTime;
$MsgType = $xmlObj->MsgType;
$Content = $xmlObj->Content;
if($MsgType=='location'){
$Location_X = $xmlObj->Location_X;
$Location_Y = $xmlObj->Location_Y;
$Scale = $xmlObj->Scale;
$Label = $xmlObj->Label;
$urlstr = "http://api.map.baidu.com/place/v2/search?&query=酒店&location=".$Location_X.",".$Location_Y."&radius=5000&output=json&ak=DESY8unmZnUlLB0mlowjuiRr"; //此處ak參數(shù)需要個人的百度開發(fā)序列號,自己去百度申請下就好了
$jsonstr = file_get_contents($urlstr);
$json = json_decode($jsonstr,true);
$pic_640 = "http://api.map.baidu.com/staticimage?width=640&height=320¢er=".$Location_Y.",".$Location_X."&zoom=15&markers=".$Location_Y.",".$Location_X."&markerStyles=l,";
$pic_80 = "http://api.map.baidu.com/staticimage?width=80&height=80¢er=".$Location_Y.",".$Location_X."&zoom=15&markers=".$Location_Y.",".$Location_X."&markerStyles=l,";
$p_640 = file_get_contents($pic_640);
file_put_contents('./images/640_'.$FromUserName.".png",$p_640);
$p_80 = file_get_contents($pic_80);
file_put_contents('./images/80_'.$FromUserName.".png",$p_80);
echo pic_send($json['results']);
}
function pic_send($arr){
global $ToUserName,$FromUserName;
$str = "<xml>
<ToUserName><![CDATA[".$FromUserName."]]></ToUserName>
<FromUserName><![CDATA[".$ToUserName."]]></FromUserName>
<CreateTime>".time()."</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>".count($arr)."</ArticleCount>
<Articles>";
foreach($arr as $k=>$v){
if($k==0){
$picurl = "http://jb51.net/weixin/images/640_".$FromUserName.".png";
}else{
$picurl = "http://jb51.net.net/weixin/images/80_".$FromUserName.".png";
}
$str .="
<item>
<Title><![CDATA[".$v['name']." 地址:".$v['address']." 電話:".$v['telephone']."]]></Title>
<Description><![CDATA[".$v['name']." 地址:".$v['address']." 電話:".$v['telephone']."]]></Description>
<PicUrl><![CDATA[".$picurl."]]></PicUrl>
<Url><![CDATA[http://api.map.baidu.com/place/detail?uid=".$v['uid']."&output=html&src=".$v['name']."&output=html]]></Url>
</item>";
}
$str .= "</Articles></xml>";
return $str;
}
?>
希望本文所述對大家的php程序設計有所幫助。
相關文章
PHP實現(xiàn)數(shù)組和對象的相互轉換操作示例
這篇文章主要介紹了PHP實現(xiàn)數(shù)組和對象的相互轉換操作,結合實例形式分析了php使用get_object_vars以數(shù)組形式訪問對象的方法,以及對象與數(shù)組相互轉換操作技巧,需要的朋友可以參考下2019-03-03
thinkphp中連接oracle時封裝方法無法用的解決辦法
本篇文章是對thinkphp中連接oracle時封裝方法無法用的解決方法進行了詳細的分析介紹,需要的朋友參考下2013-06-06
利用客戶端緩存對網(wǎng)站進行優(yōu)化的原理分析
你的網(wǎng)站在并發(fā)訪問很大并且無法承受壓力的情況下,你會選擇如何優(yōu)化?2008-09-09

