微信查詢城市天氣,首先,你需要找到一個獲取天氣的API,這裡,用的是百度的apistore,可以去百度申請和使用API。依照它的範例來請求城市天氣了。腳本裡。
#如果能回正常的資料了,那麼就可以放到你的網域空間裡了。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); // 执行HTTP请求 curl_setopt($ch , CURLOPT_URL , $url); $res = curl_exec($ch); $res = json_decode($res, true); echo "<pre class="brush:php;toolbar:false">"; print_r($res); echo ""; $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 .= "风力:" . $v['WS'] . "\n"; $contentStr .= "风向:" . $v['WD'] . "\n"; $contentStr .= "日出时间:" . $v['sunrise'] . "\n"; $contentStr .= "日落时间:" . $v['sunset'] . "\n"; $contentStr .= "经度:" . $v['longitude'] . "\n"; $contentStr .= "纬度:" . $v['latitude']; } } echo $contentStr;
以上是微信開發之帶你快速查詢城市天氣的詳細內容。更多資訊請關注PHP中文網其他相關文章!