最新微信对接及天气预报功能源代码

  1 <meta http-equiv="Content-Type" content="text/html; charset=utf8" />
  2 <?php
  3 header("Content-Type:text/html;charset=UTF-8");
  4 date_default_timezone_set("PRC");
  5 /**
  6 * 最开始用的是微信提供的demo老是不成功,用了这个网上下载的才成功
  7 */
  8 
  9 //define your token
 10 define("TOKEN", "djjc");
 11 $wechatObj = new wechatCallbackapiTest();
 12 //微信接入操作,成功接入了直接注释了就行了
 13 $wechatObj->responseMsg();
 14 //$wechatObj->valid();
 15 
 16 class wechatCallbackapiTest
 17 {
 18 /*public function valid()
 19 {
 20 $echoStr = $_GET["echostr"];
 21 
 22 //valid signature , option
 23 if($this->checkSignature()){
 24 echo $echoStr;
 25 exit;
 26 }
 27 }*/
 28 
 29 public function responseMsg()
 30 {
 31 //get post data, May be due to the different environments
 32 $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
 33 
 34 //extract post data
 35 if (!empty($postStr)){
 36 
 37 $postObj = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA);
 38 $RX_TYPE = trim($postObj->MsgType);
 39 
 40 switch($RX_TYPE)
 41 {
 42 case "text":
 43 $resultStr = $this->handleText($postObj);
 44 break;
 45 case "event":
 46 $resultStr = $this->handleEvent($postObj);
 47 break;
 48 default:
 49 $resultStr = "Unknow msg type: ".$RX_TYPE;
 50 break;
 51 }
 52 echo $resultStr;
 53 }else {
 54 echo "";
 55 exit;
 56 }
 57 }
 58 //测试文字回复
 59 public function handleText($postObj)
 60 {
 61 $fromUsername = $postObj->FromUserName;
 62 $toUsername = $postObj->ToUserName;
 63 $keyword = trim($postObj->Content);
 64 $time = time();
 65 $textTpl = "<xml>
 66 <ToUserName><![CDATA[%s]]></ToUserName>
 67 <FromUserName><![CDATA[%s]]></FromUserName>
 68 <CreateTime>%s</CreateTime>
 69 <MsgType><![CDATA[%s]]></MsgType>
 70 <Content><![CDATA[%s]]></Content>
 71 <FuncFlag>0</FuncFlag>
 72 </xml>"; 
 73 if(!empty( $keyword ))
 74 {
 75 $msgType = "text";
 76 switch($keyword){
 77 case "你好":
 78 $contentStr = "你好!";
 79 break;
 80 case "你叫什么名字":
 81 $contentStr = "我是顶尖机器人";
 82 break;
//判断是否为天气
83 case $keywords+"天气"; 84 $str = mb_substr($keyword,-2,2,"UTF-8"); 85 $str_key = mb_substr($keyword,0,-2,"UTF-8"); 86 if($str=="天气"){ 87 $data = $this->weather($str_key)->showapi_res_body; 88 $data1=‘[今天白天]‘.$data->f1->day_weather."\n"; 89 $data2=‘[今天夜间]‘.$data->f1->night_weather."\n"; 90 $data3=‘[明天白天]‘.$data->f2->day_weather."\n"; 91 $data4=‘[明天夜间]‘.$data->f2->night_weather."\n"; 92 $contentStr = $data1.$data2.$data3.$data4; 93 } 94 break; 95 default: 96 $contentStr = "听不懂您在讲什么"; 97 break; 98 } 99 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); 100 echo $resultStr; 101 }else{ 102 echo "Input something..."; 103 } 104 } 105 106 public function handleEvent($object) 107 { 108 $contentStr = ""; 109 switch ($object->Event) 110 { 111 case "subscribe": 112 $contentStr = "感谢您关注顶尖教程网,在这里您将得到海量免费学习资源!"; 113 break; 114 default : 115 $contentStr = "Unknow Event: ".$object->Event; 116 break; 117 } 118 $resultStr = $this->responseText($object, $contentStr); 119 return $resultStr; 120 } 121 122 public function responseText($object, $content) 123 { 124 $textTpl = "<xml> 125 <ToUserName><![CDATA[%s]]></ToUserName> 126 <FromUserName><![CDATA[%s]]></FromUserName> 127 <CreateTime>%s</CreateTime> 128 <MsgType><![CDATA[text]]></MsgType> 129 <Content><![CDATA[%s]]></Content> 130 <FuncFlag>0</FuncFlag> 131 </xml>"; 132 $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag); 133 return $resultStr; 134 } 135 136 private function checkSignature() 137 { 138 $signature = $_GET["signature"]; 139 $timestamp = $_GET["timestamp"]; 140 $nonce = $_GET["nonce"]; 141 142 $token = TOKEN; 143 $tmpArr = array($token, $timestamp, $nonce); 144 sort($tmpArr); 145 $tmpStr = implode( $tmpArr ); 146 $tmpStr = sha1( $tmpStr ); 147 148 if( $tmpStr == $signature ){ 149 return true; 150 }else{ 151 return false; 152 } 153 } 154 //天气预报 此处汉字需要处理,想了很多办法才没乱码 155 private function weather($k){ 156 $n=urlencode($k); 157 $showapi_appid = ‘7440‘; //去相关网站申请就行 158 $showapi_sign = ‘1900d19e5af4470ca611c7e26fd145ea‘; 159 $showapi_timestamp = date(‘YmdHis‘); 160 $areaid=‘0‘; 161 $paramArr = ‘0‘; 162 $needHourData=‘0‘; 163 $url = iconv(‘gbk‘,‘utf-8‘,‘http://route.showapi.com/9-2?‘.‘area=‘.$n.‘&areaid=&needHourData=0&needIndex=0&needMoreDay=0&showapi_appid=‘.$showapi_appid.‘&showapi_timestamp=‘.$showapi_timestamp.‘&showapi_sign=‘.$showapi_sign); 164 $result = file_get_contents($url); 165 $result = json_decode($result); 166 return $result; 167 } 168 169 } 170 171 ?>

 

最新微信对接及天气预报功能源代码

上一篇:Linux 不同方法查看进程消耗CPU IO 等


下一篇:植物大战僵尸:手工计算偏移地址