這篇文章主要介紹了微信開發(fā)如何實(shí)現(xiàn)天氣預(yù)報(bào)功能,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
成都創(chuàng)新互聯(lián)專業(yè)提供雅安電信機(jī)房服務(wù),為用戶提供五星數(shù)據(jù)中心、電信、雙線接入解決方案,用戶可自行在線購買雅安電信機(jī)房服務(wù),并享受7*24小時(shí)金牌售后服務(wù)。
1.登錄百度ak申請(qǐng):http://www.php.cn/
2.實(shí)現(xiàn)天氣信息功能
Weather.php
$ak, 'location' => $location, 'output' => $output ); $querystring = http_build_query($querystring_arrays); //生成sn $sn = md5(urlencode($uri.'?'.$querystring.$sk)); $targetUrl = sprintf($url,$ak,urlencode($location),$output,$sn); $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,$targetUrl); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); $result = curl_exec($ch); curl_close($ch); $result = json_decode($result,true); if($result["error"]!=0){ return $result["status"]; } $curHour = (int)date('H',time()); $weather = $result["results"][0]; $weatherArray[]=array("Title"=>$weather['currentCity']."天氣預(yù)報(bào)","Description"=>"","PicUrl"=>"","Url"=>""); for($i = 0;$i$weather["weather_data"][$i]["data"]."\n". $weather["weather_data"][$i]["weather"]. $weather["weather_data"][$i]["wind"]. $weather["weather_data"][$i]["temperature"], "Description"=>"", "PicUrl"=>(($curHour>=6)&&($curHour< 18))?$weather["weather_data"][$i]["dayPictureUrl"]:$weather["weather_data"][$i]["nightPictureUrl"],"URL"=>"" ); } return $weatherArray; }
3.實(shí)現(xiàn)天氣消息事件
responseMsg(); }else{ $wechatObj->valid(); } class wechatCallbackapiTest { //驗(yàn)證簽名 public function valid() { $echoStr = $_GET["echostr"]; if($this->checkSignature()){ header('content-type:text'); echo $echoStr; exit; } } public 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; } } //響應(yīng)消息 public function responseMsg() { $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!empty($postStr)){ $this->logger("R ".$postStr); $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $RX_TYPE = trim($postObj->MsgType); //消息類型分離 switch ($RX_TYPE) { case "event": $result = $this->receiveEvent($postObj); break; case "text": $result = $this->receiveText($postObj); break; default: $result = "unknown msg type: ".$RX_TYPE; break; } echo $result; }else { echo ""; exit; } } //接收事件消息 public function receiveEvent($object) { $content = ""; switch ($object->Event) { case "subscribe": $content = "歡迎關(guān)注Nicky的公眾號(hào) "; $content .= (!empty($object->EventKey))?("\n來自二維碼場(chǎng)景 ".str_replace("qrscene_","",$object->EventKey)):""; break; case "unsubscribe": $content = "取消關(guān)注"; break; } $result = $this->transmitText($object, $content); return $result; } //接收文本消息 public function receiveText($object) { $keyword = trim($object->Content); //自動(dòng)回復(fù)模式 if (strstr($keyword, "天氣")){ $city = str_replace('天氣','',$keyword); include("weather.php"); $content = getWeatherInfo($city); } $result = $this->transmitNews($object, $content); return $result; } //回復(fù)圖文消息 public function transmitNews($object, $newsArray) { if(!is_array($newsArray)){ return; } $itemTpl = "- "; $item_str = ""; foreach ($newsArray as $item){ $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']); } $xmlTpl = "
"; $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray)); return $result; } //日志記錄 public function logger($log_content) { if(isset($_SERVER['HTTP_APPNAME'])){ //SAE sae_set_display_errors(false); sae_debug($log_content); sae_set_display_errors(true); }else if($_SERVER['REMOTE_ADDR'] != "127.0.0.1"){ //LOCAL $max_size = 10000; $log_filename = "log.xml"; if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);} file_put_contents($log_filename, date('H:i:s')." ".$log_content."\r\n", FILE_APPEND); } } } ?> %s %s $item_str
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“微信開發(fā)如何實(shí)現(xiàn)天氣預(yù)報(bào)功能”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!