当前位置:首页 » 编程语言 » 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-05-05 17:19:54 浏览:46
java读取文件字符 发布:2024-05-05 17:15:18 浏览:10
三星怎么应用加密 发布:2024-05-05 17:13:18 浏览:151
cad字体在那个文件夹 发布:2024-05-05 17:08:20 浏览:329
什么时候用编译器 发布:2024-05-05 17:08:20 浏览:764
应急救援脚本 发布:2024-05-05 17:08:17 浏览:336
我的世界搭建无正版验证服务器 发布:2024-05-05 17:03:48 浏览:817
我的世界服务器地址宝可梦 发布:2024-05-05 17:00:16 浏览:254
dede企业源码 发布:2024-05-05 16:57:53 浏览:786
如何查看java版本 发布:2024-05-05 16:45:05 浏览:494