Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP版简单微信机器人源码 Read More http://cyrilis.com/20121117.html
<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "你的token");
$wechatObj = new wechatCallbackapiTest();
//验证微信的时候记得把下面的注释取消。还有最后两行添加注释。
//$wechatObj->valid();
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);
$city=array('北京','朝阳','顺义','怀柔','通州','昌平','延庆','丰台','石景山','大兴','房山','密云','门头沟','平谷','八达岭','佛爷顶','汤河口','密云上甸子','斋堂','霞云岭','北京城区','海淀','天津','宝坻','东丽','西青','北辰','蓟县','汉沽','静海','津南','塘沽','大港','武清','宁河','上海','宝山','嘉定','南汇','浦东','青浦','松江','奉贤','崇明','徐家汇','闵行','金山','石家庄','张家口','承德','唐山','秦皇岛','沧州','衡水','邢台','邯郸','保定','廊坊','郑州','新乡','许昌','平顶山','信阳','南阳','开封','洛阳','商丘','焦作','鹤壁','濮阳','周口','漯河','驻马店','三门峡','济源','安阳','合肥','芜湖','淮南','马鞍山','安庆','宿州','阜阳','亳州','黄山','滁州','淮北','铜陵','宣城','六安','巢湖','池州','蚌埠','杭州','舟山','湖州','嘉兴','金华','绍兴','台州','温州','丽水','衢州','宁波','重庆','合川','南川','江津','万盛','渝北','北碚','巴南','长寿','黔江','万州天城','万州龙宝','涪陵','开县','城口','云阳','巫溪','奉节','巫山','潼南','垫江','梁平','忠县','石柱','大足','荣昌','铜梁','璧山','丰都','武隆','彭水','綦江','酉阳','秀山','沙坪坝','永川','福州','泉州','漳州','龙岩','晋江','南平','厦门','宁德','莆田','三明','兰州','平凉','庆阳','武威','金昌','嘉峪关','酒泉','天水','武都','临夏','合作','白银','定西','张掖','广州','惠州','梅州','汕头','深圳','珠海','佛山','肇庆','湛江','江门','河源','清远','云浮','潮州','东莞','中山','阳江','揭阳','茂名','汕尾','韶关','南宁','柳州','来宾','桂林','梧州','防城港','贵港','玉林','百色','钦州','河池','北海','崇左','贺州','贵阳','安顺','都匀','兴义','铜仁','毕节','六盘水','遵义','凯里','昆明','红河','文山','玉溪','楚雄','普洱','昭通','临沧','怒江','香格里拉','丽江','德宏','景洪','大理','曲靖','保山','呼和浩特','乌海','集宁','通辽','阿拉善左旗','鄂尔多斯','临河','锡林浩特','呼伦贝尔','乌兰浩特','包头','赤峰','南昌','上饶','抚州','宜春','鹰潭','赣州','景德镇','萍乡','新余','九江','吉安','武汉','黄冈','荆州','宜昌','恩施','十堰','神农架','随州','荆门','天门','仙桃','潜江','襄樊','鄂州','孝感','黄石','咸宁','成都','自贡','绵阳','南充','达州','遂宁','广安','巴中','泸州','宜宾','内江','资阳','乐山','眉山','凉山','雅安','甘孜','阿坝','德阳','广元','攀枝花','银川','中卫','固原','石嘴山','吴忠','西宁','黄南','海北','果洛','玉树','海西','海东','济南','潍坊','临沂','菏泽','滨州','东营','威海','枣庄','日照','莱芜','聊城','青岛','淄博','德州','烟台','济宁','泰安','西安','延安','榆林','铜川','商洛','安康','汉中','宝鸡','咸阳','渭南','太原','临汾','运城','朔州','忻州','长治','大同','阳泉','晋中','晋城','吕梁','乌鲁木齐','石河子','昌吉','吐鲁番','库尔勒','阿拉尔','阿克苏','喀什','伊宁','塔城','哈密','和田','阿勒泰','阿图什','博乐','克拉玛依','拉萨','山南','阿里','昌都','那曲','日喀则','林芝','台北县','高雄','台中','海口','三亚','东方','临高','澄迈','儋州','昌江','白沙','琼中','定安','屯昌','琼海','文昌','保亭','万宁','陵水','西沙','南沙岛','乐东','五指山','琼山','长沙','株洲','衡阳','郴州','常德','益阳','娄底','邵阳','岳阳','张家界','怀化','黔阳','永州','吉首','湘潭','南京','镇江','苏州','南通','扬州','宿迁','徐州','淮安','连云港','常州','泰州','无锡','盐城','哈尔滨','牡丹江','佳木斯','绥化','黑河','双鸭山','伊春','大庆','七台河','鸡西','鹤岗','齐齐哈尔','大兴安岭','长春','延吉','四平','白山','白城','辽源','松原','吉林','通化','沈阳','鞍山','抚顺','本溪','丹东','葫芦岛','营口','阜新','辽阳','铁岭','朝阳','盘锦','大连','锦州');
if(in_array($keyword, $city)){
$urlstr = file_get_contents("http://sou.qq.com/online/get_weather.php?callback=Weather&city=".$keyword);
$strgsh="[".substr($urlstr,8,-2)."]";
$arrayjson=json_decode($strgsh,true);
if($arrayjson[0]['future']['forecast'][3]['BWEA']==$arrayjson[0]['future']['forecast'][3]['EWEA']){
$hwea=$arrayjson[0]['future']['forecast'][3]['BWEA'];
}else{
$hwea=$arrayjson[0]['future']['forecast'][3]['BWEA']."".$arrayjson[0]['future']['forecast'][3]['EWEA'];
}
if($arrayjson[0]['future']['forecast'][4]['BWEA']==$arrayjson[0]['future']['forecast'][4]['EWEA']){
$dhwea=$arrayjson[0]['future']['forecast'][4]['BWEA'];
}else{
$dhwea=$arrayjson[0]['future']['forecast'][4]['BWEA']."".$arrayjson[0]['future']['forecast'][4]['EWEA'];
}
$huifu="城市:".$arrayjson[0]['future']['name']."
现在气温是".$arrayjson[0]['real']['temperature']."℃。
今日天气:".$arrayjson[0]['future']['wea_0']."
温度范围:".$arrayjson[0]['future']['forecast'][0]['TMAX']."~".$arrayjson[0]['future']['forecast'][0]['TMIN']."
明天天气:".$arrayjson[0]['future']['wea_1']."
后天天气:".$arrayjson[0]['future']['wea_2']."
大后天的:".$hwea."
大大后天:".$dhwea;
}
else if(strpos($keyword,"你好")!==false){
$huifu="你也好~";
}
else if(strpos($keyword,"")!==false){
$huifu="我不傻!哼~ ";
}
else if(strpos($keyword,"")!==false){
$huifu="你说脏话,我叫警察叔叔来抓你! ";
}
else if(strpos($keyword,"")!==false){
$huifu="你说脏话,我叫警察叔叔来抓你! 哼~ ";
}
else if(strpos($keyword,"帮助")!==false){
$huifu="输入城市名称查天气就好啦! 比如输入”北京“";
}
else if(strpos($keyword,"是谁")!==false){
$huifu="我是微天气, 你可以叫我小微,可以叫我小天,但不要叫我小气~";
}
else if(strpos($keyword,"小气")!==false){
$huifu="你才小气呐!";
}
else if($keyword=="h"||$keyword=="H")
{
$huifu="欢迎使用微天气,
输入城市名字(如:'北京')并发送即可查询天气情况。
输入'H'或者'h'显示本帮助内容。
输入'冷笑话'即可查看冷笑话一枚。
输入'a'查看程序相关。
输入v查看版本号。";
}
else if($keyword=='a'){
$huifu="本程序是由Cyril 创建。
http://cyrilis.com
欢迎提出改进意见。:)";
}
else if($keyword=="v")
{
$huifu="版本号:0.91";
}
else if($keyword=="冷笑话")
{
$huifu="冷笑话功能近期推出哟~";
}
else{
$huifu="太复杂了我看不懂,
说点儿我看得懂的吧,
输入城市名(如“北京”或者“青岛”)查看天气,
或输入'h'查看帮助。";
}
$time = time();
$textTpl = "<xml>
<ToUserName><![CDATA[".$fromUsername."]]></ToUserName>
<FromUserName><![CDATA[".$toUsername."]]></FromUserName>
<CreateTime>".$time."</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[".$huifu."]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
if(!empty( $keyword ))
{
$msgType = "text";
$contentStr = "Welcome to wechat world!";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $textTpl;
}else{
$msgType = "text";
$contentStr = "Welcome to wechat world!";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $textTpl;
}
}else {
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;
}
}
}
//验证微信的时候记得把下面两行注释掉。
$dick = new wechatCallbackapiTest();
$dick->responseMsg();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.