PHP微信開發之根據使用者回複關鍵詞\位置返回附近資訊_php執行個體

來源:互聯網
上載者:User

使用者關注了微信公眾號之後,可以回複使用者的地理位置(騰訊地圖)給公眾號,提取位置資訊的緯度和經度,轉化為百度的緯度和經度。然後根據緯度和經度,調用百度地圖的API,返回附近半徑2KM以內的“飯店”“旅館”(可以自訂)等資訊。調用百度的API時,需要擷取apiKEY,如果沒有,請到百度開發人員中心去註冊和申請。

首先,用一組緯度和經度來測試介面返回的資料:

<?php/**根據一組經緯度尋找附近2公裡以內的關鍵字**/header('Content-type:text/html;charset=utf-8');//--------第一步:轉換經緯度----//參考連結:http://developer.baidu.com/map/index.php?title=webapi/guide/changeposition$Location_X = 23.134521;$Location_Y = 113.358803;$url = "http://api.map.baidu.com/geoconv/v1/?coords=$Location_X,$Location_Y&from=3&to=5&ak=這裡填寫你的apikey";$res = file_get_contents($url);$res = json_decode($res, true);//使用者發送騰訊的soso地圖位置資訊經過轉換之後,得到百度地圖的經緯度$x = $res['result'][0]['x'];$y = $res['result'][0]['y'];//---------第二步:根據經緯度和關鍵詞獲得附近的資訊----------//參考連結:http://developer.baidu.com/map/index.php?title=webapi/guide/webservice-placeapi$url = "http://api.map.baidu.com/place/v2/search?ak=這裡填寫你的apikey&output=json&query=" . urlencode("飯店") . "&page_size=10&page_num=0&scope=2&location=$x,$y&radius=2000";$res = file_get_contents($url);$res = json_decode($res, true);// echo "<pre>";// print_r($res);// echo "</pre>"; //提取所需的資訊foreach($res['results'] as $k=>$v){ $arr[$k]['name'] = $v['name']; $arr[$k]['address'] = $v['address']; $arr[$k]['img_url'] = 'http://misakaqnxt-public.stor.sinaapp.com/click.png'; $arr[$k]['detail_url'] = isset($v['detail_info']['detail_url'])?$v['detail_info']['detail_url']:'';} echo "<pre>"; print_r($arr); echo "</pre>"; 

返回的資料

 

如果你填寫了正確的apikey,那麼應該返回了上面的資料了。接下來:在微信的介面平台代碼(放在你的公網網域名稱空間裡的PHP指令碼)裡,判斷訊息類型,並擷取緯度和經度,調用百度地圖API,拼接XML返回即可。由於百度API返回的資料裡,沒有“飯店”的縮圖,所以我就用了自己網站的一張圖。
 為了能夠讓使用者自訂尋找周圍的“飯店”
 或“旅館”等資訊,可以先讓使用者回複“尋找XX”,然後提取出XX,放到session裡,等使用者再回複地理位置時取出session。但我設定了session之後,沒能取出session。所以我這裡用新浪雲的KVDB服務,當然你也可以用memcache或者Redis等緩衝方式。 

$which = mb_substr($keyword, 0, 2, 'UTF-8');

elseif($which == "尋找"){        $find = str_replace($which, "", $keyword);        //調用新浪雲的KVDB服務        $kv = new SaeKV();        $kv->init();        $kv->set('find', $find);        $contentStr = "選擇表情旁邊的'+',發送位置,即可尋找你要找的地方";        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgtype, $contentStr);        echo $resultStr;        exit();      }
 elseif($postObj->MsgType == 'location'){          /**           * 如果是收到了地理位置訊息,則返回附近的飯店           */          //--------第一步:轉換經緯度-------          //參考連結:http://developer.baidu.com/map/index.php?title=webapi/guide/changeposition          $Location_X = $postObj->Location_X;          $Location_Y = $postObj->Location_Y;                    $url = "http://api.map.baidu.com/geoconv/v1/?coords=$Location_X,$Location_Y&from=3&to=5&ak=這裡填寫你的apikey";                    $res = file_get_contents($url);          $res = json_decode($res, true);          //使用者發送騰訊的soso地圖位置資訊經過轉換之後,得到百度地圖的經緯度          $x = $res['result'][0]['x'];          $y = $res['result'][0]['y'];          //---------第二步:根據經緯度和關鍵詞獲得附近的資訊----------          $kv = new SaeKV();          // 初始化KVClient對象          $kv->init();                    $url = "http://api.map.baidu.com/place/v2/search?ak=這裡填寫你的apikey&output=json&query=" . urlencode($kv->get('find')) . "&page_size=10&page_num=0&scope=2&location=$x,$y&radius=2000";          $res = file_get_contents($url);          $res = json_decode($res, true);                    //提取資訊          foreach($res['results'] as $k=>$v){            $arr[$k]['name'] = $v['name'];            $arr[$k]['address'] = $v['address'];            $arr[$k]['img_url'] = 'http://misakaqnxt-public.stor.sinaapp.com/click.png';            $arr[$k]['detail_url'] = isset($v['detail_info']['detail_url'])?$v['detail_info']['detail_url']:'';          }          //--------第三步:拼接XML字串--------          $head = "<xml>          <ToUserName><![CDATA[%s]]></ToUserName>          <FromUserName><![CDATA[%s]]></FromUserName>          <CreateTime>%s</CreateTime>          <MsgType><![CDATA[news]]></MsgType>          <ArticleCount>10</ArticleCount>          <Articles>";          $items = "";          foreach($arr as $v){            $items .= "<item>            <Title><![CDATA[" . $v['name'] .":". $v['address'] . "]]></Title>            <Description><![CDATA[" . $v['address'] . "]]></Description>            <PicUrl><![CDATA[" . $v['img_url'] . "]]></PicUrl>            <Url><![CDATA[" . $v['detail_url'] . "]]></Url>            </item>";          }          $foot = "</Articles></xml>";          $res = $head . $items . $foot;                    $resultStr = sprintf($res, $fromUsername, $toUsername, $time);          echo $resultStr;          exit();} 

如果你看不懂代碼怎麼用,可以參考我之前的文章:簡單的文本回複    查詢微信精選文章 

使用者關注了公眾號之後,回複尋找飯店,然後回複地理位置之後,就能得附近的飯店資訊了。

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.