《PHP實(shí)戰(zhàn):PHP微信開(kāi)發(fā)之根據(jù)用戶回復(fù)關(guān)鍵詞\位置返回附近信息》要點(diǎn):
本文介紹了PHP實(shí)戰(zhàn):PHP微信開(kāi)發(fā)之根據(jù)用戶回復(fù)關(guān)鍵詞\位置返回附近信息,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
PHP學(xué)習(xí)用戶關(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,如果沒(méi)有,請(qǐng)到百度開(kāi)發(fā)者中心去注冊(cè)和申請(qǐng).
PHP學(xué)習(xí)首先,用一組緯度和經(jīng)度來(lái)測(cè)試接口返回的數(shù)據(jù):
PHP學(xué)習(xí)
<?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)過(guò)轉(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>";
PHP學(xué)習(xí)返回的數(shù)據(jù)
PHP學(xué)習(xí)?
PHP學(xué)習(xí)如果你填寫了正確的apikey,那么應(yīng)該返回了上面的數(shù)據(jù)了.接下來(lái):在微信的接口平臺(tái)代碼(放在你的公網(wǎng)域名空間里的PHP腳本)里,判斷消息類型,并獲取緯度和經(jīng)度,調(diào)用百度地圖API,拼接X(jué)ML返回即可.由于百度API返回的數(shù)據(jù)里,沒(méi)有“飯店”的縮略圖,所以我就用了自己網(wǎng)站的一張圖.
?為了能夠讓用戶自定義查找周圍的“飯店”
?或“旅館”等信息,可以先讓用戶回復(fù)“尋找XX”,然后提取出XX,放到session里,等用戶再回復(fù)地理位置時(shí)取出session.但我設(shè)置了session之后,沒(méi)能取出session.所以我這里用新浪云的KVDB服務(wù),當(dāng)然你也可以用memcache或者Redis等緩存方式.?
PHP學(xué)習(xí)$which = mb_substr($keyword, 0, 2, 'UTF-8');
PHP學(xué)習(xí)
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();
}
PHP學(xué)習(xí)
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)過(guò)轉(zhuǎn)換之后,得到百度地圖的經(jīng)緯度
$x = $res['result'][0]['x'];
$y = $res['result'][0]['y'];
//---------第二步:根據(jù)經(jīng)緯度和關(guān)鍵詞獲得附近的信息----------
$kv = new SaeKV();
// 初始化KVClient對(duì)象
$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']:'';
}
//--------第三步:拼接X(jué)ML字符串--------
$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學(xué)習(xí)如果你看不懂代碼怎么用,可以參考我之前的文章:簡(jiǎn)單的文本回復(fù)??? 查詢微信精選文章?
PHP學(xué)習(xí)用戶關(guān)注了公眾號(hào)之后,回復(fù)尋找飯店,然后回復(fù)地理位置之后,就能得附近的飯店信息了.
PHP學(xué)習(xí)以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持維易PHP.?
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/6100.html