å¾®ä¿¡å ¬ä¼å¹³å°å¼åä¹å¤©æ°é¢æ¥åè½_phpå®ä¾
项ç®éæ±ï¼æè¿æ¥å¥½ç微信平å°,æç¾åº¦æ³¨åå¸å·,éè¦å¨ç¾åº¦LBSå¼æ¾äºå¹³å°,æ·»å åºç¨,è·åAK代ç ï¼PHP代ç ç¼è¾å¨,å¦EditPlusç
ä¸é¢è¯¦ç»ä»ç»ä¸å¼åæ¥éª¤ï¼
第ä¸æ¥ï¼åå¤å·¥ä½
ç»å½å¾®ä¿¡å ¬ä¼å¹³å°,æ£æ¥æå¡å¨é ç½®æ¯å¦å·²å¯ç¨,URL(æå¡å¨å°å) æ¯å¦å·²é ç½®Token(令ç),ä¸èªå·±åçå¾®ä¿¡å ¥å£æ件ä¸çToken(令çä¸è´),å¦ä¸å¾:ç¶åç¹å»æ交,åªè³ç½é¡µä¸æ示绿è²èæ¯çæ交æåä¿¡æ¯,åå®ææ¬æ¥éª¤çæä½
第äºæ¥ï¼å¾®ä¿¡å¤©æ°é¢æ¥æ°æ®æºåå¤
ç¨å·²æ³¨å好çç¾åº¦å¸å·,ç»å½ç¾åº¦LBSäºå¹³å°,æ·»å ä¸ä¸ªåºç¨,è·å访é®åºç¨AKï¼åäºè§£è½¦èAPI V3.0,天æ°æ¥è¯¢åè½ç¸åºçæ¥å£è¯´ææ件,以æéè°ç¨éè¦ç天æ°ä¿¡æ¯.
第ä¸æ¥ï¼å¾®ä¿¡å ¬ä¼å¹³å°ï¼æ¥å£æ件ç¼å jiekou.php
responseMsg(); }else{ $wechatObj->valid(); } class wechatCallbackapiTest { //éªè¯ç¾å public function valid() { $echoStr = $_GET["echostr"]; $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){ echo $echoStr; exit; } } public function responseMsg() { // $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; $postStr = file_get_contents("php://input"); if (!empty($postStr)){ $this->logger("R ".$postStr); $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $RX_TYPE = trim($postObj->MsgType); $result = ""; switch ($RX_TYPE) { case "event": $result = $this->receiveEvent($postObj); break; case "text": $result = $this->receiveText($postObj); break; } $this->logger("T ".$result); echo $result; }else { echo ""; exit; } } private function receiveEvent($object) { switch ($object->Event) { case "subscribe": $content = "欢è¿å ³æ³¨æ 忧çµèæå·§ç½ "; break; } $result = $this->transmitText($object, $content); return $result; } private function receiveText($object) { $keyword = trim($object->Content); //è·å¾ç¨æ·è¾å ¥çä¿¡æ¯ //å¤æå¤©æ° if(!empty( $keyword )){ //!empty å½æ°,å¤æ $keywordè·å¾çå¼æ¯å¦ä¸ºç©º $city = mb_substr($keyword, 0, 2, 'utf-8'); //åç¨æ·è¾å ¥å 容å两个å符,å¦"é»å天æ°" æç»åå¼"é»å" include("weather.php"); //è°ç¨å¤©æ°æ¥å£æ件 $content = getWeatherInfo($city); //æ§è¡å¤©æ°æ¥å£æ件ä¸ç getWeatherInfoæ¹æ³.æ¥è¯¢ é»å天æ°. } else{ $content = date("Y-m-d H:i:s",time())."\nææ¯æ¯æ æ 忧çµèæå·§ç½\nwww.51pcjq.com"; //åéå ¶å®å 容é»è®¤åå¤çå 容. } if(is_array($content)){ if (isset($content[0]['PicUrl'])){ $result = $this->transmitNews($object, $content); }else if (isset($content['MusicUrl'])){ $result = $this->transmitMusic($object, $content); } }else{ $result = $this->transmitText($object, $content); } return $result; } private function transmitText($object, $content) { if (!isset($content) || empty($content)){ return ""; } $textTpl = ""; $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content); return $result; } private function transmitNews($object, $newsArray) { if(!is_array($newsArray)){ return ""; } $itemTpl = " %s - "; $item_str = ""; foreach ($newsArray as $item){ $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']); } $newsTpl = "
"; $result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray)); return $result; } private function logger($log_content) { } } %s %s $item_str
第åæ¥:使ç¨ç¾åº¦è½¦èAPI V3.0æ¥å£,å访é®åºç¨AKç ,ç¼å·å¾®ä¿¡å¤©æ°æ¥å£æºç :
weatger.php
$weather['currentCity']."å½å天æ°:"."温度:".$weather['weather_data'][0]['temperature'].",".$weather['weather_data'][0]['weather'].","."é£å:".$weather['weather_data'][0]['wind'].".", "Description" =>"", "PicUrl" =>"http://weixin.51pcjq.com/img/weather_bg.jpg", "Url" =>""); for ($i = 0; $i $weather["weather_data"][$i]["date"]."\n". $weather["weather_data"][$i]["weather"]." ". $weather["weather_data"][$i]["wind"]." ". $weather["weather_data"][$i]["temperature"]."", "Description"=>"", "PicUrl"=>(($curHour >= 6) && ($curHour ""); } return $weatherArray;}?>
注æäºé¡¹
å¾®ä¿¡å ¬ä¼å¹³å° TOKEN ä¸èªå·±ç¼åç微信æ¥å£æ件ä¸ç TOKEN çå¼å¿ é¡»ä¿æä¸è´
ç¼åphp代ç ,é使ç¨ä¸ä¸çphpç¼è¾å·¥å ·,å¦EditPlus,Dreamweaverç
ä¸è¿°å¤©æ°æ¥å£æ件ä¸,ç¾åº¦è½¦èapi AK代ç å·²æ¢æ "èªå·²ç³è¯·çç¾åº¦è½¦èAPI AK代ç :请ç³è¯·å¥½å,èªè¡å¡«å ¥
å¦è¦ä¸æç½çå°æ¹,å¯ä»¥å ³æ³¨æçç¾åº¦ç©ºé´.çè¨åæèç³»!
微信天æ°é¢æ¥å¼åææ°ä»£ç ï¼
responseMsg(); //$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); $RX_TYPE = trim($postObj->MsgType); switch($RX_TYPE) { case "text": $resultStr = $this->handleText($postObj); break; case "event": $resultStr = $this->handleEvent($postObj); break; default: $resultStr = "Unknow msg type: ".$RX_TYPE; break; } echo $resultStr; }else { echo ""; exit; } } //æµè¯æååå¤ public function handleText($postObj) { $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $textTpl = ""; if(!empty( $keyword )) { $msgType = "text"; switch($keyword){ case "ä½ å¥½": $contentStr = "ä½ å¥½!"; break; case "ä½ å«ä»ä¹åå": $contentStr = "ææ¯é¡¶å°æºå¨äºº"; break; //å¤ææ¯å¦ä¸ºå¤©æ° case $keywords+"天æ°"; $str = mb_substr($keyword,-,,"UTF-"); $str_key = mb_substr($keyword,,-,"UTF-"); if($str=="天æ°"){ $data = $this->weather($str_key)->showapi_res_body; $data=â[ä»å¤©ç½å¤©]â.$data->f->day_weather."\n"; $data=â[ä»å¤©å¤é´]â.$data->f->night_weather."\n"; $data=â[æ天ç½å¤©]â.$data->f->day_weather."\n"; $data=â[æ天å¤é´]â.$data->f->night_weather."\n"; $contentStr = $data.$data.$data.$data; } break; default: $contentStr = "å¬ä¸ææ¨å¨è®²ä»ä¹"; break; } $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }else{ echo "Input something..."; } } public function handleEvent($object) { $contentStr = ""; switch ($object->Event) { case "subscribe": $contentStr = "æè°¢æ¨å ³æ³¨é¡¶å°æç¨ç½ï¼å¨è¿éæ¨å°å¾å°æµ·éå è´¹å¦ä¹ èµæºï¼"; break; default : $contentStr = "Unknow Event: ".$object->Event; break; } $resultStr = $this->responseText($object, $contentStr); return $resultStr; } public function responseText($object, $content) { $textTpl = " %s "; $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag); return $resultStr; } 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 = sha( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } //天æ°é¢æ¥ æ¤å¤æ±åéè¦å¤çï¼æ³äºå¾å¤åæ³æ没乱ç private function weather($k){ $n=urlencode($k); $showapi_appid = ââ; //å»ç¸å ³ç½ç«ç³è¯·å°±è¡ $showapi_sign = âdeafcacefdeaâ; $showapi_timestamp = date(âYmdHisâ); $areaid=ââ; $paramArr = ââ; $needHourData=ââ; $url = iconv(âgbkâ,âutf-â,âhttp://route.showapi.com/-?â.âarea=â.$n.â&areaid=&needHourData=&needIndex=&needMoreDay=&showapi_appid=â.$showapi_appid.â&showapi_timestamp=â.$showapi_timestamp.â&showapi_sign=â.$showapi_sign); $result = file_get_contents($url); $result = json_decode($result); return $result; } } ?> %s