<?php
/***微信api接口处理函数****/
//天气查询函数
function _getWeather($keyword){
$cityname = trim(substr($keyword,6,strlen($keyword)-6));
$url = "http://api.map.baidu.com/telematics/v2/weather?location={$cityname}&ak=1a3cde429f38434f1811a75e1a90310c";
$fa=file_get_contents($url);
$f=simplexml_load_string($fa);
$city=$f->currentCity;
$da1=$f->results->result[0]->date;
$da2=$f->results->result[1]->date;
$da3=$f->results->result[2]->date;
$w1=$f->results->result[0]->weather;
$w2=$f->results->result[1]->weather;
$w3=$f->results->result[2]->weather;
$p1=$f->results->result[0]->wind;
$p2=$f->results->result[1]->wind;
$p3=$f->results->result[2]->wind;
$q1=$f->results->result[0]->temperature;
$q2=$f->results->result[1]->temperature;
$q3=$f->results->result[2]->temperature;
$d1=$cityname.$da1.$w1.$p1.$q1;
$d2=$cityname.$da2.$w2.$p2.$q2;
$d3=$cityname.$da3.$w3.$p3.$q3;
$msg =<<<str
$d1
$d2
$d3
str;
return $msg;
}
//翻译函数
function _fanyi($keyword){
$keyword = trim(substr($keyword,6,strlen($keyword)-6));
$tranurl="http://openapi.baidu.com/public/2.0/bmt/translate?client_id=9peNkh97N6B9GGj9zBke9tGQ&q={$keyword}&from=auto&to=auto";//百度翻译地址
$transtr=file_get_contents($tranurl);//读入文件
$transon=json_decode($transtr);//json解析
//print_r($transon);
$contentStr = $transon->trans_result[0]->dst;//读取翻译内容
return $contentStr;
}
//快递查询函数
function _getDindan($keyword){
$keyword = trim(substr($keyword,6,strlen($keyword)-6));
$status=array('0'=>'查询出错','1'=>'暂无记录','2'=>'在途中','3'=>'派送中','4'=>'已签收','5'=>'拒收','6'=>'疑难件','7'=>'退回');//构建快递状态数组
$kuaidiurl="http://www.aikuaidi.cn/rest/?key=ff4735a30a7a4e5a8637146fd0e7cec9&order={$keyword}&id=shentong&show=xml";//快递地址
$kuaidistr=file_get_contents($kuaidiurl);//读入文件
$kuaidiobj=simplexml_load_string($kuaidistr);//xml解析
$kuaidistatus = $kuaidiobj->Status;//获取快递状态
$kuaistr=strval($kuaidistatus);//对象转换为字符串
$contentStr0 =$status[$kuaistr];//根据数组返回
foreach ($kuaidiobj->Data->Order as $a)
{
foreach ($a->Time as $b)
{
foreach ($a->Content as $c)
{$m.="{$b}{$c}";}
}
}
//遍历获取快递时间和事件
$contentStr="你的快递单号{$keyword}{$contentStr0}{$m}";
return $contentStr;
}
//小黄鸡函数
function _xiaohuangji($keyword){
$keyword = trim(substr($keyword,1,strlen($keyword)-1));
$strurl="http://sandbox.api.simsimi.com/request.p?key=e0f1c913-fe3a-40ad-904f-5467677a38b7&lc=ch&text='{$keyword}'";
$fa=file_get_contents($strurl);
$strjson=json_decode($fa);
$contentStr = $strjson->response;
return $contentStr;
}
?>用的方法就是 //微信首页方法
public function index(){
/* 加载微信SDK */
import('@.ORG.ThinkWechat');
$weixin = new ThinkWechat('yangyifan');
/* 获取请求信息 */
$data = $weixin->request();
/* 获取回复信息 */
list($content, $type) = $this->reply($data);
M('info')->data($data)->add();
//天气
if(substr($data['Content'],0,6) == '天气'){
$content = _getWeather($data['Content']);
$type = 'text';
//翻译
}else if(substr($data['Content'],0,6) == '翻译'){
$content = _fanyi($data['Content']);
$type = 'text';
//快递
}else if(substr($data['Content'],0,6) == '快递'){
$content = _getDindan($data['Content']);
$type = 'text';
//小黄鸡
}else if(substr($data['Content'],0,1) == '@'){
$content = _xiaohuangji($data['Content']);
$type = 'text';
//帮助
}else{
$content = '';
}
$weixin->response($content, $type);
}
ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。