欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP微信開發(fā)之根據(jù)用戶回復(fù)關(guān)鍵詞\位置返回附近信息

 更新時(shí)間:2016年06月24日 09:01:02   作者:misaka去年夏天  
這篇文章主要為大家詳細(xì)介紹了PHP微信開發(fā)之簡單實(shí)現(xiàn)根據(jù)用戶回復(fù)關(guān)鍵詞\位置返回附近信息 ,感興趣的小伙伴們可以參考一下

用戶關(guān)注了微信公眾號(hào)之后,可以回復(fù)用戶的地理位置(騰訊地圖)給公眾號(hào),提取位置信息的緯度和經(jīng)度,轉(zhuǎn)化為百度的緯度和經(jīng)度。然后根據(jù)緯度和經(jīng)度,調(diào)用百度地圖的API,返回附近半徑2KM以內(nèi)的“飯店”“旅館”(可以自定義)等信息。調(diào)用百度的API時(shí),需要獲取apiKEY,如果沒有,請到百度開發(fā)者中心去注冊和申請。

首先,用一組緯度和經(jīng)度來測試接口返回的數(shù)據(jù):

<?php

/**根據(jù)一組經(jīng)緯度查找附近2公里以內(nèi)的關(guān)鍵字**/

header('Content-type:text/html;charset=utf-8');

//--------第一步:轉(zhuǎn)換經(jīng)緯度----
//參考鏈接: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);

//用戶發(fā)送騰訊的soso地圖位置信息經(jīng)過轉(zhuǎn)換之后,得到百度地圖的經(jīng)緯度
$x = $res['result'][0]['x'];
$y = $res['result'][0]['y'];

//---------第二步:根據(jù)經(jīng)緯度和關(guān)鍵詞獲得附近的信息----------
//參考鏈接: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>"; 

返回的數(shù)據(jù)

 

如果你填寫了正確的apikey,那么應(yīng)該返回了上面的數(shù)據(jù)了。接下來:在微信的接口平臺(tái)代碼(放在你的公網(wǎng)域名空間里的PHP腳本)里,判斷消息類型,并獲取緯度和經(jīng)度,調(diào)用百度地圖API,拼接XML返回即可。由于百度API返回的數(shù)據(jù)里,沒有“飯店”的縮略圖,所以我就用了自己網(wǎng)站的一張圖。
 為了能夠讓用戶自定義查找周圍的“飯店”
 或“旅館”等信息,可以先讓用戶回復(fù)“尋找XX”,然后提取出XX,放到session里,等用戶再回復(fù)地理位置時(shí)取出session。但我設(shè)置了session之后,沒能取出session。所以我這里用新浪云的KVDB服務(wù),當(dāng)然你也可以用memcache或者Redis等緩存方式。 

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

elseif($which == "尋找"){

        $find = str_replace($which, "", $keyword);

        //調(diào)用新浪云的KVDB服務(wù)
        $kv = new SaeKV();
        $kv->init();
        $kv->set('find', $find);

        $contentStr = "選擇表情旁邊的'+',發(fā)送位置,即可查找你要找的地方";
        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgtype, $contentStr);
        echo $resultStr;
        exit();

      }

 elseif($postObj->MsgType == 'location'){
          /**
           * 如果是收到了地理位置消息,則返回附近的飯店
           */

          //--------第一步:轉(zhuǎn)換經(jīng)緯度-------
          //參考鏈接: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);

          //用戶發(fā)送騰訊的soso地圖位置信息經(jīng)過轉(zhuǎn)換之后,得到百度地圖的經(jīng)緯度
          $x = $res['result'][0]['x'];
          $y = $res['result'][0]['y'];

          //---------第二步:根據(jù)經(jīng)緯度和關(guān)鍵詞獲得附近的信息----------
          $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();


} 

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

用戶關(guān)注了公眾號(hào)之后,回復(fù)尋找飯店,然后回復(fù)地理位置之后,就能得附近的飯店信息了。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。 

相關(guān)文章

最新評論