您的位置:首页 > 移动开发 > 微信开发

获取PM2.5的微信公共账号

2013-03-25 19:49 225 查看
这是一个获取PM2.5的微信公共账号,由于没有公开的API,我就自己弄了个,大家可以上http://52tlife.duapp.com看看



上源码:

<?php
/**
* wechat php test
*/

//define your token
define("TOKEN", "ijob");
$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;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
if(!empty( $keyword ))
{
$msgType = "text";
//$contentStr = "Welcome to wechat world!";
$stock=new Stock();
$results=$stock->get_stock($keyword);
$contentStr="PM2.5:".$results['pm']."\n"."污染程度:".$results['pollute']."\n"."温馨提示:".$results['tl'];
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else{
echo "Input something...";
}

}else {
echo "abc";
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 Stock
{
public function get_stock($value)
{
$qurl='http://iopenapi.duapp.com/pm.php?city='.$value;//可以参考http://52tlife.duapp.com
$fetch=new BaeFetchUrl();
$content=$fetch->get($qurl);//解析网页
$result=json_decode($content,true);//json数据的解析
return array('pm'=>$result['pm'],'pollute'=>$result['pollute'],'tl'=>$result['tl']);//返回数组的形式
}
}
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: