云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > 微信公众平台PHP开发基于地理位置的服务

微信公众平台PHP开发基于地理位置的服务

  • 62 次阅读
  • 3 次下载
  • 2026/1/27 10:14:11

微信公众平台开发小应用:用经纬度坐标位

置查询周围餐厅

// weixin.class.php

classWeixin { public $token = ''; //token

public $debug = false; //是否debug的状态标示,方便我们在调试的时候记录一些中间数据 public $setFlag = false; public $msgtype = 'text'; //('text','image','location') public $msg = array(); public function __construct($token,$debug) { $this->token = $token; $this->debug = $debug; } //获得用户发过来的消息(消息内容和消息类型) public function getMsg() { $postStr = $GLOBALS[\ if ($this->debug) { $this->write_log($postStr); } if (!empty($postStr)) { $this->msg = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $this->msgtype = strtolower($this->msg['MsgType']); } } //回复文本消息 public function makeText($text='') { $CreateTime = time(); $FuncFlag = $this->setFlag ?1 : 0;

$textTpl = \ msg['FromUserName']}]]> msg['ToUserName']}]]> {$CreateTime} %s \ returnsprintf($textTpl,$text,$FuncFlag); } //根据数组参数回复图文消息 public function makeNews($newsData=array()) { $CreateTime = time(); $FuncFlag = $this->setFlag ?1 : 0; $newTplHeader = \ msg['FromUserName']}]]> msg['ToUserName']}]]> {$CreateTime} %s\ $newTplItem = \ <![CDATA[%s]]> \ $newTplFoot = \ %s \ $Content = ''; $itemsCount = count($newsData['items']); $itemsCount = $itemsCount<10 ? $itemsCount : 10;//微信公众平台图文回复的消息一次最多10条 if ($itemsCount) { foreach ($newsData['items'] as $key => $item) { if ($key<=9) { $Content .= sprintf($newTplItem,$item['title'],$item['description'],$item['picurl'],$item['url']); } } }

$header = sprintf($newTplHeader,$newsData['content'],$itemsCount); $footer = sprintf($newTplFoot,$FuncFlag); return $header . $Content . $footer; }

public function reply($data) { if ($this->debug) { $this->write_log($data); } echo $data; }

public function valid() { if ($this->checkSignature()) { if( $_SERVER['REQUEST_METHOD']=='GET' ) { echo $_GET['echostr']; exit; } }else{ write_log('认证失败'); exit; } }

private function checkSignature() { $signature = $_GET[\ $timestamp = $_GET[\ $nonce = $_GET[\ $tmpArr = array($this->token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } }

private function write_log($log){

//这里是你记录调试信息的地方请自行完善以便中间调试 } } ?>

// zhaofanguan.php

include_once('weixin.class.php');//引用刚定义的微信消息处理类 define(\define('DEBUG', true);

define('GMAP_API_KEY', \

$weixin = new Weixin(TOKEN,DEBUG);//实例化 $weixin->getMsg();

$type = $weixin->msgtype;//消息类型 $username = $weixin->msg['FromUserName'];//哪个用户给你发的消息,这个$username是微信加密之后的,但是每个用户都是一一对应的 if($type==='location') { $lat = $weixin->msg['Location_X']; $lng = $weixin->msg['Location_Y']; // Google Map API $url = \ \. $lng . \ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_REFERER, \ $body = curl_exec($ch); curl_close($ch); $json = json_decode($body); $results = (array)$json->results; $restaurants=\ foreach ($results as $result) { $restaurants= $result->name .\ } $reply = $weixin->makeText(“亲,您周围1000米的餐馆有:\ }

$weixin->reply($reply); ?>

  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

微信公众平台开发小应用:用经纬度坐标位置查询周围餐厅 // weixin.class.php token = $token; $this

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com