這篇文章給大家分享的是有關(guān)PHP微信開發(fā)之查詢城市天氣的示例分析的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
PHP微信查詢城市天氣,首先,你需要找到一個(gè)獲取天氣的API,此處,我用的是百度的apistore,申請(qǐng)和使用API的網(wǎng)址:http://apistore.baidu.com/apiworks/servicedetail/112.html
登錄百度賬號(hào),并用手機(jī)發(fā)送請(qǐng)求獲取apikey。有了apikey,可以按照它的示例來(lái)請(qǐng)求城市天氣了。(可以按照城市中文名,拼音,城市編號(hào)等來(lái)查詢)
你可以現(xiàn)在本地做測(cè)試,請(qǐng)求完成之后,再放到自己的域名空間的腳本里。
測(cè)試的腳本例如:(注意apikey填寫自己申請(qǐng)的)
header('Content-type:text/html;charset=UTF-8'); $ch = curl_init(); $url = 'http://apis.baidu.com/apistore/weatherservice/cityname?cityname=上海'; $header = array( 'apikey: ',//你的apikey ); // 添加apikey到header curl_setopt($ch, CURLOPT_HTTPHEADER , $header); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 執(zhí)行HTTP請(qǐng)求 curl_setopt($ch , CURLOPT_URL , $url); $res = curl_exec($ch); $res = json_decode($res, true); echo "<pre>"; print_r($res); echo "</pre>"; $contentStr = ""; foreach($res as $k=>$v){ if($k == "retData"){ $contentStr = "城市:" . $v['city'] . "\n"; $contentStr .= "日期:" . $v['date'] . "\n"; $contentStr .= "天氣:" . $v['weather'] ."\n"; $contentStr .= "平均氣溫:" . $v['temp'] . "℃\n"; $contentStr .= "最低氣溫:" . $v['l_tmp'] ."℃\n"; $contentStr .= "高氣溫:" . $v['h_tmp'] . "℃\n"; $contentStr .= "風(fēng)力:" . $v['WS'] . "\n"; $contentStr .= "風(fēng)向:" . $v['WD'] . "\n"; $contentStr .= "日出時(shí)間:" . $v['sunrise'] . "\n"; $contentStr .= "日落時(shí)間:" . $v['sunset'] . "\n"; $contentStr .= "經(jīng)度:" . $v['longitude'] . "\n"; $contentStr .= "緯度:" . $v['latitude']; } } echo $contentStr;
如果你填寫了自己的apikey,那么應(yīng)該能獲取到所請(qǐng)求的天氣了:
如果能返回正常的數(shù)據(jù)了,那么就可以放到你的域名空間里了。(公眾平臺(tái)里開發(fā)者中心填寫的url,該url有連接微信接口等功能)
如果你看不懂下面的代碼或者第一次接觸微信開發(fā),可以參考我之前的文章:https://www.jb51.net/article/87252.htm
下面的代碼是responseMsg的一部分:
public function responseMsg(){ <span > </span>//get post data, May be due to the different environments $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //接收微信發(fā)來(lái)的XML數(shù)據(jù) //extract post data if(!empty($postStr)){ //解析post來(lái)的XML為一個(gè)對(duì)象$postObj $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; //請(qǐng)求消息的用戶 $toUsername = $postObj->ToUserName; //"我"的公眾號(hào)id $keyword = trim($postObj->Content); //用戶發(fā)送的消息內(nèi)容 $time = time(); //時(shí)間戳 $msgtype = 'text'; //消息類型:文本 $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; if($postObj->MsgType == 'event'){ //如果XML信息里消息類型為event if($postObj->Event == 'subscribe'){ //如果是訂閱事件 $contentStr = "歡迎訂閱misaka去年夏天!\n更多精彩內(nèi)容:http://blog.csdn.net/misakaqunianxiatian"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgtype, $contentStr); echo $resultStr; exit(); } } $which = mb_substr($keyword, 0, 2, 'UTF-8');//獲取要返回什么樣的信息 if($which== "翻譯"){ //如果要進(jìn)行翻譯 //調(diào)用有道翻譯API進(jìn)行翻譯 }elseif($which == "天氣"){ $wea = $which; $city = str_replace($wea, "", $keyword); $ch = curl_init(); $url = 'http://apis.baidu.com/apistore/weatherservice/cityname?cityname=' . $city; $header = array('apikey: '); //此處的apikey使用自己申請(qǐng)的apikey,填在冒號(hào)之后 // 添加apikey到header curl_setopt($ch, CURLOPT_HTTPHEADER , $header); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 執(zhí)行HTTP請(qǐng)求 curl_setopt($ch , CURLOPT_URL , $url); $res = curl_exec($ch); $res = json_decode($res, true); $contentStr = ""; foreach($res as $k=>$v){ if($k == "retData"){ $contentStr = "城市:" . $v['city'] . "\n"; $contentStr .= "日期:" . $v['date'] . "\n"; $contentStr .= "天氣:" . $v['weather'] ."\n"; $contentStr .= "平均氣溫:" . $v['temp'] . "℃\n"; $contentStr .= "最低氣溫:" . $v['l_tmp'] ."℃\n"; $contentStr .= "高氣溫:" . $v['h_tmp'] . "℃\n"; $contentStr .= "風(fēng)力:" . $v['WS'] . "\n"; $contentStr .= "風(fēng)向:" . $v['WD'] . "\n"; $contentStr .= "日出時(shí)間:" . $v['sunrise'] . "\n"; $contentStr .= "日落時(shí)間:" . $v['sunset'] . "\n"; $contentStr .= "經(jīng)度:" . $v['longitude'] . "\n"; $contentStr .= "緯度:" . $v['latitude']; } } $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgtype, $contentStr); echo $resultStr; exit(); }else{ $contentStr = "輸入翻譯XXX可以進(jìn)行翻譯(=?ω?=)\n\n輸入天氣XX可以查詢城市天氣"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgtype, $contentStr); echo $resultStr; exit(); }
完成之后(別忘了填寫apikey),你的訂閱號(hào)里,輸入天氣上海,那么應(yīng)該能查到上海當(dāng)天的天氣了。
感謝各位的閱讀!關(guān)于“PHP微信開發(fā)之查詢城市天氣的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
當(dāng)前標(biāo)題:PHP微信開發(fā)之查詢城市天氣的示例分析-創(chuàng)新互聯(lián)
標(biāo)題路徑:http://chinadenli.net/article44/pphee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、全網(wǎng)營(yíng)銷推廣、移動(dòng)網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、標(biāo)簽優(yōu)化、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容