《PHP實戰:PHP微信開發之根據用戶回復關鍵詞\位置返回附近信息》要點:
本文介紹了PHP實戰:PHP微信開發之根據用戶回復關鍵詞\位置返回附近信息,希望對您有用。如果有疑問,可以聯系我們。
PHP學習用戶關注了微信公眾號之后,可以回復用戶的地理位置(騰訊地圖)給公眾號,提取位置信息的緯度和經度,轉化為百度的緯度和經度.然后根據緯度和經度,調用百度地圖的API,返回附近半徑2KM以內的“飯店”“旅館”(可以自定義)等信息.調用百度的API時,需要獲取apiKEY,如果沒有,請到百度開發者中心去注冊和申請.
PHP學習首先,用一組緯度和經度來測試接口返回的數據:
PHP學習
<?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>";
PHP學習返回的數據
PHP學習?
PHP學習如果你填寫了正確的apikey,那么應該返回了上面的數據了.接下來:在微信的接口平臺代碼(放在你的公網域名空間里的PHP腳本)里,判斷消息類型,并獲取緯度和經度,調用百度地圖API,拼接XML返回即可.由于百度API返回的數據里,沒有“飯店”的縮略圖,所以我就用了自己網站的一張圖.
?為了能夠讓用戶自定義查找周圍的“飯店”
?或“旅館”等信息,可以先讓用戶回復“尋找XX”,然后提取出XX,放到session里,等用戶再回復地理位置時取出session.但我設置了session之后,沒能取出session.所以我這里用新浪云的KVDB服務,當然你也可以用memcache或者Redis等緩存方式.?
PHP學習$which = mb_substr($keyword, 0, 2, 'UTF-8');
PHP學習
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();
}
PHP學習
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();
}
PHP學習如果你看不懂代碼怎么用,可以參考我之前的文章:簡單的文本回復??? 查詢微信精選文章?
PHP學習用戶關注了公眾號之后,回復尋找飯店,然后回復地理位置之后,就能得附近的飯店信息了.
PHP學習以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持維易PHP.?
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/6100.html