php微信公眾開發之擷取周邊酒店資訊的方法_php技巧

來源:互聯網
上載者:User

本文執行個體講述了php微信公眾開發之擷取周邊酒店資訊的方法。分享給大家供大家參考。具體分析如下:

關注微信公眾之後發送回複地理位置資訊,即可回複周邊附近的酒店資訊列表,下面我就來給各位介紹利用php是怎麼實現這個功能,希望全子對大家有協助.代碼如下:

複製代碼 代碼如下:
<?php
 //將提交過來的資訊接收
$signature = $_GET['signature'];
 $timestamp = $_GET['timestamp'];
 $nonce = $_GET['nonce'];
 $echostr = $_GET['echostr'];
 $token = "jb51.net";
 //判斷接入網站
 
 //進行字典排序
 $arr = array($token,$timestamp,$nonce);
 
 sort($arr);
 
 //完成字串的拼接和sha1加密
 $result = sha1(join($arr));
 //判斷產生的字串和$signature是否相等,如果相等,直接輸出$echostr,這樣網站接入成功
 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參數需要個人的百度開發序號,自己去百度申請下就好了
 $jsonstr = file_get_contents($urlstr);
 $json = json_decode($jsonstr,true);
 
 $pic_640 = "http://api.map.baidu.com/staticimage?width=640&height=320&center=".$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&center=".$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 = "yun_qi_img/640_.$FromUserName.png";
 }else{
 $picurl = "yun_qi_img/q=jb51";
 }
 $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程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.