Home  >  Article  >  Backend Development  >  PHP微信开发之根据用户回复关键词位置返回附近信息,php关键词_PHP教程

PHP微信开发之根据用户回复关键词位置返回附近信息,php关键词_PHP教程

WBOY
WBOYOriginal
2016-07-11 10:36:211000browse

PHP微信开发之根据用户回复关键词\位置返回附近信息,php关键词

用户关注了微信公众号之后,可以回复用户的地理位置(腾讯地图)给公众号,提取位置信息的纬度和经度,转化为百度的纬度和经度。然后根据纬度和经度,调用百度地图的API,返回附近半径2KM以内的“饭店”“旅馆”(可以自定义)等信息。调用百度的API时,需要获取apiKEY,如果没有,请到百度开发者中心去注册和申请。

首先,用一组纬度和经度来测试接口返回的数据:

<&#63;php

/**根据一组经纬度查找附近2公里以内的关键字**/

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

//--------第一步:转换经纬度----
//参考链接:http://developer.baidu.com/map/index.php&#63;title=webapi/guide/changeposition

$Location_X = 23.134521;
$Location_Y = 113.358803;

$url = "http://api.map.baidu.com/geoconv/v1/&#63;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&#63;title=webapi/guide/webservice-placeapi
$url = "http://api.map.baidu.com/place/v2/search&#63;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 class="brush:php;toolbar:false">";
// print_r($res);
// echo "
"; //提取所需的信息 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 "
";
 print_r($arr);
 echo "
";

返回的数据

 

如果你填写了正确的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&#63;title=webapi/guide/changeposition
          $Location_X = $postObj->Location_X;
          $Location_Y = $postObj->Location_Y;
          
          $url = "http://api.map.baidu.com/geoconv/v1/&#63;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&#63;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'])&#63;$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();


} 

如果你看不懂代码怎么用,可以参考我之前的文章:简单的文本回复    查询微信精选文章 

用户关注了公众号之后,回复寻找饭店,然后回复地理位置之后,就能得附近的饭店信息了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持帮客之家。 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1138990.htmlTechArticlePHP微信开发之根据用户回复关键词\位置返回附近信息,php关键词 用户关注了微信公众号之后,可以回复用户的地理位置(腾讯地图)给公...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn