用户
 找回密码
 入住 CI 中国社区
搜索
查看: 2975|回复: 3
收起左侧

CI框架如何调用API数据

[复制链接]
发表于 2017-10-12 16:42:44 | 显示全部楼层 |阅读模式
链接一个天气预报的api接口,但是不知道如何调用。我是php小白~求助大神
json数据链接在此:http://www.sojson.com/open/api/weather/json.shtml?city=%E5%8D%97%E9%98%B3

发表于 2017-10-12 17:07:58 | 显示全部楼层
PHP复制代码
 
$data = file_get_contents('http://www.sojson.com/open/api/weather/json.shtml?city=%E5%8D%97%E9%98%B3');
echo '<pre>';
var_dump(json_decode($data));
echo '</pre>';
 
复制代码


json_decode第二个参数为TRUE时,返回的是数组,而不是对象。
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2017-10-12 18:01:03 | 显示全部楼层
wangyouworld 发表于 2017-10-12 17:07
json_decode第二个参数为TRUE时,返回的是数组,而不是对象。

我获取到数据,并且转换成数组了,我该怎么获取它单独的元素数据:
这是我获取的数据
  1. Array ( [date] => 20171012 [message] => Success ! [status] => 200 [city] => 南阳 [count] => 24 [data] => Array ( [shidu] => 69% [pm25] => 9 [pm10] => 26 [quality] => 优 [wendu] => 14 [ganmao] => 各类人群可自由活动 [yesterday] => Array ( [date] => 11日星期三 [sunrise] => 06:30 [high] => 高温 13.0℃ [low] => 低温 11.0℃ [sunset] => 18:03 [aqi] => 29 [fx] => 北风 [fl] => 3-4级 [type] => 大雨 [notice] => 请尽量避免出门 ) [forecast] => Array ( [0] => Array ( [date] => 12日星期四 [sunrise] => 06:31 [high] => 高温 16.0℃ [low] => 低温 11.0℃ [sunset] => 18:01 [aqi] => 34 [fx] => 无持续风向 [fl] => <3级 [type] => 阴 [notice] => 灰蒙蒙的天空就像模糊了的眼睛 ) [1] => Array ( [date] => 13日星期五 [sunrise] => 06:31 [high] => 高温 18.0℃ [low] => 低温 13.0℃ [sunset] => 18:00 [aqi] => 57 [fx] => 无持续风向 [fl] => <3级 [type] => 阴 [notice] => 灰蒙蒙的天空就像模糊了的眼睛 ) [2] => Array ( [date] => 14日星期六 [sunrise] => 06:32 [high] => 高温 16.0℃ [low] => 低温 13.0℃ [sunset] => 17:59 [aqi] => 65 [fx] => 无持续风向 [fl] => <3级 [type] => 小雨 [notice] => 外出时请注意关好门窗,防止雨水飘入 ) [3] => Array ( [date] => 15日星期日 [sunrise] => 06:33 [high] => 高温 16.0℃ [low] => 低温 13.0℃ [sunset] => 17:58 [aqi] => 62 [fx] => 无持续风向 [fl] => <3级 [type] => 中雨 [notice] => 今日中雨,出门请带好雨具 ) [4] => Array ( [date] => 16日星期一 [sunrise] => 06:34 [high] => 高温 16.0℃ [low] => 低温 12.0℃ [sunset] => 17:57 [aqi] => 42 [fx] => 无持续风向 [fl] => <3级 [type] => 小雨 [notice] => 下雨了不要紧,撑伞挡挡就行 ) ) ) )
复制代码
发表于 2017-10-12 18:39:26 | 显示全部楼层
曹逗逗 发表于 2017-10-12 18:01
我获取到数据,并且转换成数组了,我该怎么获取它单独的元素数据:
这是我获取的数据 ...

建议先看看 PHP 基础。

本版积分规则