<?php
header('Content-type:text/html;charset=utf-8');
/**
* 说明:微信智能处理
* 作者:rlm0909@live.com
* 时间:2013.11.07
* 版本:V1.0.0.0
*/
//这里是你申请是写的 token
define("TOKEN", "rlm0909@live.com");
$wechatObj = new wechatCallbackapiTest();
//首次使用,请注释掉下面的一行代码。
//然后添加
$wechatObj->valid(); //供首次验证需要
//验证成功后,即可以使用下面的代码
//$wechatObj->responseMsg();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
public function responseMsg()
{
//get post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//extract post data
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$time = time();
if($postObj->MsgType!='text'){
$contentStr = "欢迎加入我们
作者:rlm0909@live.com
";
}else{
$keyword = trim($postObj->Content);
}
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
if (!isset($contentStr)) {
$getweather = new getweather();
$contentStr=$getweather->getw($keyword);
}
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr);
echo $resultStr;
}else {
echo "请输入信息处理";
exit;
}
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
class getweather{
//天气的对应编码,可以通过文件引入
private $arr=array(
101010100=>'北京',
);
//天气对应编码的另一种形式
private $arrs=array(
101010100=>'beijing',
);
public function getw($str){
$code=$this->check($str);
if ($code){
$content = file_get_contents("http://m.weather.com.cn/data/{$code}.html");
if(!$content){
//mysql_query("insert into errorlog values(null,'没有取到天气接口数据','$date',$time)",$link);
//echo mysql_error();
file_put_contents('weather.txt', '没有接受到天气数据', FILE_APPEND);
return '暂时没有取到天气数据,请稍后再试';
}
$result=json_decode($content,true);
$info=$result['weatherinfo'];
$strw="{$info['date_y']}{$info['week']},{$info['city']}的天气情况\n";
$strw .= "今天:({$info['temp1']}){$info['weather1']}{$info['wind1']}{$info['fl1']}。";
$strw .= "24小时穿衣指数:{$info['index_d']}\n";
$strw .= "明天:({$info['temp2']}){$info['weather2']}{$info['wind2']}{$info['fl2']}。";
$strw .= "48小时穿衣指数:{$info['index48_d']}";
return $strw;
}else{
return '没有获取到该城市的天气,请确定输入了正确的城市名称,如\'北京\'或者\'beijing\'';
}
}
/**
* 验证接受到的数据是否合法
* @param string $str 传入的接受到的数据
* @return mixed|boolean 有数据返回$code,没有数据返回false;
*
*/
private function check($str){
if (in_array($str, $this->arr)) {
return array_search($str, $this->arr);
}elseif (in_array($str, $this->arrs)){
return array_search($str, $this->arrs);
}else {
return false;
}
}
}