當前位置:首頁 » 編程語言 » php天氣api

php天氣api

發布時間: 2022-08-15 18:00:35

Ⅰ 怎麼利用php的api獲得城市新浪天氣

<?php
//發送瀏覽器頭信息告訴瀏覽器本頁面的編碼防止亂碼
header("Content-type:text/html;charset=utf-8");
$city='上海';
//定義介面地址
$encode_url='http://php.weather.sina.com.cn/xml.php?city='.urlencode(mb_convert_encoding($city,'gb2312','utf8')).'&password=DJOYnieT8234jlsK&day=1';
//請求介面地址獲取xml
$xml_source=file_get_contents($encode_url);
//var_mp($xml_source);

//利用simplexml解析類將xml轉成對象
$xml_object=simplexml_load_string($xml_source);
//var_mp($xml_object);
echo$xml_object->Weather->status1;

你可以隨便調試 記得獎勵哦

Ⅱ 現在可用的天氣API還有哪些新浪外,要PHP可用,最好有實例。

支持直接jsonp調用 騰訊的 :
http://sou.qq.com/online/get_weather.php?callback=Weather&city=nanjing(這個很好 直接輸入天氣名字就行 英文中文都行)

中國天氣http://-weather.com.cn (先查城市id 再查天氣)

http://61.4.185.48:81/g/ (根據IP查詢城市ID)
http://m.weather.com.cn/data/101110101.html(六天預報)
http://www.weather.com.cn/data/sk/101110101.html(實時天氣信息)

Ⅲ php的socket調用可以實現查天氣嗎

本文分享下,php調用yahoo與sina的天氣api,實現實時顯示天氣預報的代碼,有興趣的朋友研究下吧。

yahoo 天氣預報
地址 http://developer.yahoo.com/weather/
代碼:

復制代碼代碼示例:
<?php
header ( 'Content-Type: text/html; charset = utf-8' );
class weather {
static $url = 'http://xml.weather.yahoo.com/forecastrss?u=c&w=';
static $city = 'Beijing'; //默認城市北京 這里要注意的是 city 要填拼音 我試過用中文有好幾個地區都調用不到
static $weatherXML = '';
static $woeid_file = "woeid";
static $file_path = "data/";

/**
* 獲得遠程xml並緩存到本地
*/
static public function getXML($city = null) {

if ($city != null){
self::$city = $city;
}
self::$weatherXML = self::$file_path . md5(self::$city) . '-weather.xml';
if (file_exists( self::$weatherXML )) {
$fileTime = filemtime ( self::$weatherXML );
$stater = time () - $fileTime - 60 * 60 * 2;
if ($stater > 0) {
return true;
}
}
//獲取woeid
$woeid = self::getWOEID();
self::$url = self::$url . $woeid[0];
//獲取當天 天氣
$XML = self::vget(self::$url);
//保存當天 天氣到文件
self::cacheXML($XML);

self::analysisXML($XML);
}

static public function analysisXML($simple) {

$p = xml_parser_create();

xml_parse_into_struct($p, $simple, $vals, $index);

xml_parser_free($p);

//本周天氣
$weekindex = $index['YWEATHER:FORECAST'];
$week = array();
foreach($weekindex as $k=>$v){
$week[$k] = $vals[$v]['attributes'];
}
unset($index);
unset($vals);
print_r($week);
/*
<yweather:forecast day="Wed" date="18 Sep 2013" low="20" high="32" text="Sunny" code="32"/>
* day 星期
* date 日期
* low 最低溫度
* high 最高溫度
* test 天氣狀態
* code 天氣圖標
*/
}
/*
* 取得地區WOEID碼
*/
static private function getWOEID(){
static $woeid = array();

if(isset($woeid[self::$city])){
return $woeid[self::$city];
}

if (file_exists( self::$file_path . self::$woeid_file )) {
$woeidSTR = file_get_contents(self::$file_path . self::$woeid_file);
$woeid = json_decode($woeidSTR , true);
if(isset($woeid[self::$city])){
return $woeid[self::$city];
}
}
$geoPlaces = "http://query.yahooapis.com/v1/public/yql?q=select%20woeid%20from%20geo.places%20where%20text='".self::$city."%20CH'";

$XML = simplexml_load_file( $geoPlaces );

if(isset($XML->results->place[0])){
$rs = $woeid[self::$city] = $XML->results->place[0]->woeid;
//保存到文件
$woeidSTR = json_encode($woeid);
file_put_contents(self::$file_path . self::$woeid_file, $woeidSTR);
return $rs;
}else{
//如果找不到城市 woeid 默認城市就改為 北京
self::$city = "Beijing";

return self::getWOEID();
}
}
/**
* 創建xml緩存
* @param $contents 要緩存的內容
*/
static private function cacheXML($contents) {
$contents = str_ireplace ( '<?xml version="1.0"?>', "<?xml version=\"1.0\"?> \n", $contents );
$contents = mb_convert_encoding ( $contents, 'utf-8', 'gbk' );
file_put_contents ( self::$weatherXML, $contents ) or die ( '沒有寫許可權' );
}
/**
* 模擬獲取內容函數
* @param type $url
* @return type
*/

static private function vget($url) {
$user_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";
$curl = curl_init (); // 啟動一個CURL會話
curl_setopt ( $curl, CURLOPT_URL, $url ); // 要訪問的地址
curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, 0 ); // 對認證證書來源的檢查
curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 1 ); // 從證書中檢查SSL加密演算法是否存在
curl_setopt ( $curl, CURLOPT_USERAGENT, $user_agent ); // 模擬用戶使用的瀏覽器
@curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 ); // 使用自動跳轉
curl_setopt ( $curl, CURLOPT_AUTOREFERER, 1 ); // 自動設置Referer
curl_setopt ( $curl, CURLOPT_HTTPGET, 1 ); // 發送一個常規的Post請求
curl_setopt ( $curl, CURLOPT_TIMEOUT, 120 ); // 設置超時限制防止死循環
curl_setopt ( $curl, CURLOPT_HEADER, 0 ); // 顯示返回的Header區域內容
curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); // 獲取的信息以文件流的形式返回
$tmpInfo = curl_exec ( $curl ); // 執行操作
if (curl_errno ( $curl )) {
curl_close ( $curl ); // 關閉CURL會話
die('Errno' . curl_error ( $curl )) ;
}
curl_close ( $curl ); // 關閉CURL會話
return $tmpInfo; // 返回數據
}
}
weather::getXML("Changsha");

Ⅳ 如何使用PHP調用API介面實現天氣查詢功能

最近在做微信公眾平台測試時,想在裡面子菜單上添加查詢未來幾天(包括今天)天氣的功能,就查找了下好用的天氣預報查詢介面API,使用比較多的有:國家氣象局天氣介面、新浪天氣預報介面、網路天氣預報介面、google天氣介面、Yahoo天氣介面等等,我使用的是網路提供的免費天氣查詢介面API,下面與大家分享下...

1、查詢方式:

網路提供的是根據緯度和城市名查詢天氣情況

2、介面事例:


Ⅳ php怎麼解析天氣預報api返回的數據

json就要用json的形式返回啊 就行ajax返回數據一樣 它的返回值是true 就是正確的 如果是返回ajax的話,先判斷data 為真就直接data.XX(欄位名) 就可以取出來了

Ⅵ PHP天氣api,除了新浪還有哪些可用,最好帶上實例

apistore..com/astore/classificationservicelist/70.html?sortBy=default&sortOrder=desc

這裡面可以找到很多天氣類的API,有些是收費的,有些是免費的

免費:

apistore..com/astore/serviceinfo/27744.html

API文檔見連接

Ⅶ 調用百度天氣的api時候跨域的問題

同源的問題,目前網上沒有很好的解決網路天氣api的方案。個人目前實行的方式是,先將api數據的通過file_get_contents獲取JSON字元串,然後再用ajax調用.php函數file_get_contents應對網路天氣介面

Ⅷ php如何獲得當地的天氣預報 - 技術問答

這個無需PHP做。前端就可以獲取,直接搜索天氣API,會有很多天氣介面。
http://www.weather.com.cn/data/sk/101010100.html
或者
http://cj.weather.com.cn/

Ⅸ php獲取天氣預報的代碼

<?php
$URLStyle="http://flash.weather.com.cn/wmaps/xml/%s.xml";
$chinaURL=sprintf($URLStyle,"china");
$chinaStr=file_get_contents($chinaURL);
$chinaObj=simplexml_load_string($chinaStr);
$chinaObjLen=count($chinaObj->city);
echo"chinaObjLen=".$chinaObjLen." ";
for($i=0;$i<$chinaObjLen;$i++){
//遍歷省一級節點,共37個
$level1=$chinaObj->city[$i]["pyName"];
$shengjiURL=sprintf($URLStyle,$level1);
$shengjiStr=file_get_contents($shengjiURL);
//echo$shengjiStr;
$shengjiObj=simplexml_load_string($shengjiStr);
$shengjiObjLen=count($shengjiObj->city);
//echo$chinaObj->city[$i]["quName"];
//echo"".$shengjiObjLen." ";
for($j=0;$j<$shengjiObjLen;$j++){
//遍歷市一級節點
$level2=$shengjiObj->city[$j]["pyName"];
$shijiURL=sprintf($URLStyle,$level2);
$shijiStr=file_get_contents($shijiURL);
//echo$shijiStr;
$shijiObj=simplexml_load_string($shijiStr);
//直轄市和海南、台灣、釣魚島等沒有縣級節點
if(!$shijiObj){
echo"WARNNING:notexsitnextlevelnode.-".$level1."-".$shijiURL." ";
echo'"'.$shengjiObj->city[$j]["cityname"].'"=>';
echo$shengjiObj->city[$j]["url"].", ";
continue;
}
$shijiObjLen=count($shijiObj->city);
//echo$shengjiObj->city[$j]["cityname"]."";
//echo$shijiObjLen." ";
for($k=0;$k<$shijiObjLen;$k++){
//遍歷縣一級節點
$xianji_code=$shijiObj->city[$k]["url"];
echo'"'.$shijiObj->city[$k]["cityname"].'"=>';
echo$shijiObj->city[$k]["url"].", ";
//echo$xianji_code." ";
}
}
}
//print_r($chinaObj);
?>

通過XML介面根節點遞歸獲得全國幾千個縣以上城市cide code的代碼

Ⅹ php網頁的api使用 比如我自己的一個網頁,想要使用天氣網提供的api,如何得到數據,如何處理這

介面會返回json數據,用php提供的json_decode函數可以將其轉為對象或者數組,再輸出即可。 可以參考我的網站 http://zy62.com

熱點內容
解壓到當前文件夾右鍵 發布:2024-04-26 03:57:08 瀏覽:979
html5android教程視頻下載 發布:2024-04-26 03:09:59 瀏覽:867
伺服器的描述是什麼 發布:2024-04-26 03:08:32 瀏覽:394
個人加密 發布:2024-04-26 03:01:23 瀏覽:521
linuxusbgadget 發布:2024-04-26 02:52:54 瀏覽:304
我的世界空島世界伺服器地址 發布:2024-04-26 01:39:08 瀏覽:248
尼爾機械紀元加密 發布:2024-04-26 01:37:11 瀏覽:868
在控制台輸出sql語句 發布:2024-04-26 01:08:12 瀏覽:432
動畫java 發布:2024-04-26 01:02:40 瀏覽:12
得力文件夾5302 發布:2024-04-26 00:21:32 瀏覽:91