Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 关于JSON解析错误_Php_Json_Api_Parsing_Weather Api - Fatal编程技术网

Php 关于JSON解析错误

Php 关于JSON解析错误,php,json,api,parsing,weather-api,Php,Json,Api,Parsing,Weather Api,这一直在工作的人不知什么原因突然停止了工作。我一直在使用文件获取内容。不确定是什么导致代码混乱$json=file\u get\u contents('http://api.openweathermap.org/data/2.5/weather?q=London,英国&appid=a1fc2c19d548237a56e0edd7b79b3ebc'); <?php $url = "http://api.openweathermap.org/data/2.5/weather?q=Lon

这一直在工作的人不知什么原因突然停止了工作。我一直在使用
文件获取内容
。不确定是什么导致代码混乱

$json=file\u get\u contents('http://api.openweathermap.org/data/2.5/weather?q=London,英国&appid=a1fc2c19d548237a56e0edd7b79b3ebc');
<?php
$url     = "http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=YOURAPI";
$json    = file_get_contents($url);
$data    = json_decode($json, true);
$data['city']['name'];

foreach ($data['list'] as $day => $value) {
  echo $todaystemperature = $value[temp][max];
}
?>
$data=json_decode($json,true); 或 $data=json_decode($json); 回声“; 打印(数据); 出口
您的代码似乎不正确,您使用的url给出了以下响应

    {
  "coord": {
    "lon": -0.13,
    "lat": 51.51
  },
  "weather": [
    {
      "id": 804,
      "main": "Clouds",
      "description": "overcast clouds",
      "icon": "04d"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 284.66,
    "pressure": 1016.88,
    "humidity": 68,
    "temp_min": 284.66,
    "temp_max": 284.66,
    "sea_level": 1024.55,
    "grnd_level": 1016.88
  },
  "wind": {
    "speed": 4.91,
    "deg": 97.501
  },
  "clouds": {
    "all": 92
  },
  "dt": 1476377646,
  "sys": {
    "message": 0.0048,
    "country": "GB",
    "sunrise": 1476339772,
    "sunset": 1476378553
  },
  "id": 2643743,
  "name": "London",
  "cod": 200
}
在上面的json响应中,我找不到任何带有“list”的键,这就是代码中断的原因。这段代码可以用来获取城市的温度

<?php
 $url     = "http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=a1fc2c19d548237a56e0edd7b79b3ebc";
 $json    = file_get_contents($url);
 $data    = json_decode($json, true);
 echo $temp = $data['main']['temp'];
?>

需要根据您得到的JSON响应解析JSON数据

使用PHP获取Json数据的步骤
  • 首先,您必须获得具有json_响应的数据
  • 然后,您必须使用
    json\u decode($json,TRUE)
  • 之后,您可以使用
    foreach()
    来分割基于解码值得到的数组
在运行您在代码中给出的上述URL时,我得到如下输出

array (
  'coord' => 
  array (
    'lon' => -0.13000000000000000444089209850062616169452667236328125,
    'lat' => 51.50999999999999801048033987171947956085205078125,
  ),
  'weather' => 
  array (
    0 => 
    array (
      'id' => 804,
      'main' => 'Clouds',
      'description' => 'overcast clouds',
      'icon' => '04d',
    ),
  ),
  'base' => 'stations',
  'main' => 
  array (
    'temp' => 284.66000000000002501110429875552654266357421875,
    'pressure' => 1016.8799999999999954525264911353588104248046875,
    'humidity' => 68,
    'temp_min' => 284.66000000000002501110429875552654266357421875,
    'temp_max' => 284.66000000000002501110429875552654266357421875,
    'sea_level' => 1024.549999999999954525264911353588104248046875,
    'grnd_level' => 1016.8799999999999954525264911353588104248046875,
  ),
  'wind' => 
  array (
    'speed' => 4.910000000000000142108547152020037174224853515625,
    'deg' => 97.501000000000004774847184307873249053955078125,
  ),
  'clouds' => 
  array (
    'all' => 92,
  ),
  'dt' => 1476377768,
  'sys' => 
  array (
    'message' => 0.176900000000000001687538997430237941443920135498046875,
    'country' => 'GB',
    'sunrise' => 1476339772,
    'sunset' => 1476378552,
  ),
  'id' => 2643743,
  'name' => 'London',
  'cod' => 200,
)
获得的JSON:

{"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"base":"stations","main":{"temp":284.66,"pressure":1016.88,"humidity":68,"temp_min":284.66,"temp_max":284.66,"sea_level":1024.55,"grnd_level":1016.88},"wind":{"speed":4.91,"deg":97.501},"clouds":{"all":92},"dt":1476377768,"sys":{"message":0.1769,"country":"GB","sunrise":1476339772,"sunset":1476378552},"id":2643743,"name":"London","cod":200}
<?php
 $url     = "http://api.openweathermap.org/data/2.5/weather?  q=London,uk&appid=a1fc2c19d548237a56e0edd7b79b3ebc";
 $json    = file_get_contents($url);
 $data    = json_decode($json, true);
 echo $temp = $data['main']['temp']; // To convert the data to Celsius by hitting the API called api.openweathermap. 
?>
{

    "coord":{
        "lon":-0.13,"lat":51.51},"weather":[
        {
            "id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"base":"stations","main":{
        "temp":284.66,"pressure":1016.88,"humidity":68,"temp_min":284.66,"temp_max":284.66,"sea_level":1024.55,"grnd_level":1016.88},"wind":{
        "speed":4.91,"deg":97.501},"clouds":{
        "all":92},"dt":1476377768,"sys":{
        "message":0.1769,"country":"GB","sunrise":1476339772,"sunset":1476378552},"id":2643743,"name":"London","cod":200

}
{

    "coord":{
        "lon":-0.13,"lat":51.51},"weather":[
        {
            "id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"base":"stations","main":{
        "temp":284.66,"pressure":1016.88,"humidity":68,"temp_min":284.66,"temp_max":284.66,"sea_level":1024.55,"grnd_level":1016.88},"wind":{
        "speed":4.91,"deg":97.501},"clouds":{
        "all":92},"dt":1476377768,"sys":{
        "message":0.1769,"country":"GB","sunrise":1476339772,"sunset":1476378552},"id":2643743,"name":"London","cod":200

}
因此,在后续的过程中,您可以得到如下值

PHP代码:

{"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"base":"stations","main":{"temp":284.66,"pressure":1016.88,"humidity":68,"temp_min":284.66,"temp_max":284.66,"sea_level":1024.55,"grnd_level":1016.88},"wind":{"speed":4.91,"deg":97.501},"clouds":{"all":92},"dt":1476377768,"sys":{"message":0.1769,"country":"GB","sunrise":1476339772,"sunset":1476378552},"id":2643743,"name":"London","cod":200}
<?php
 $url     = "http://api.openweathermap.org/data/2.5/weather?  q=London,uk&appid=a1fc2c19d548237a56e0edd7b79b3ebc";
 $json    = file_get_contents($url);
 $data    = json_decode($json, true);
 echo $temp = $data['main']['temp']; // To convert the data to Celsius by hitting the API called api.openweathermap. 
?>
{

    "coord":{
        "lon":-0.13,"lat":51.51},"weather":[
        {
            "id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"base":"stations","main":{
        "temp":284.66,"pressure":1016.88,"humidity":68,"temp_min":284.66,"temp_max":284.66,"sea_level":1024.55,"grnd_level":1016.88},"wind":{
        "speed":4.91,"deg":97.501},"clouds":{
        "all":92},"dt":1476377768,"sys":{
        "message":0.1769,"country":"GB","sunrise":1476339772,"sunset":1476378552},"id":2643743,"name":"London","cod":200

}
{

    "coord":{
        "lon":-0.13,"lat":51.51},"weather":[
        {
            "id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"base":"stations","main":{
        "temp":284.66,"pressure":1016.88,"humidity":68,"temp_min":284.66,"temp_max":284.66,"sea_level":1024.55,"grnd_level":1016.88},"wind":{
        "speed":4.91,"deg":97.501},"clouds":{
        "all":92},"dt":1476377768,"sys":{
        "message":0.1769,"country":"GB","sunrise":1476339772,"sunset":1476378552},"id":2643743,"name":"London","cod":200

}
Js评估:

{"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"base":"stations","main":{"temp":284.66,"pressure":1016.88,"humidity":68,"temp_min":284.66,"temp_max":284.66,"sea_level":1024.55,"grnd_level":1016.88},"wind":{"speed":4.91,"deg":97.501},"clouds":{"all":92},"dt":1476377768,"sys":{"message":0.1769,"country":"GB","sunrise":1476339772,"sunset":1476378552},"id":2643743,"name":"London","cod":200}
<?php
 $url     = "http://api.openweathermap.org/data/2.5/weather?  q=London,uk&appid=a1fc2c19d548237a56e0edd7b79b3ebc";
 $json    = file_get_contents($url);
 $data    = json_decode($json, true);
 echo $temp = $data['main']['temp']; // To convert the data to Celsius by hitting the API called api.openweathermap. 
?>
{

    "coord":{
        "lon":-0.13,"lat":51.51},"weather":[
        {
            "id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"base":"stations","main":{
        "temp":284.66,"pressure":1016.88,"humidity":68,"temp_min":284.66,"temp_max":284.66,"sea_level":1024.55,"grnd_level":1016.88},"wind":{
        "speed":4.91,"deg":97.501},"clouds":{
        "all":92},"dt":1476377768,"sys":{
        "message":0.1769,"country":"GB","sunrise":1476339772,"sunset":1476378552},"id":2643743,"name":"London","cod":200

}
{

    "coord":{
        "lon":-0.13,"lat":51.51},"weather":[
        {
            "id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"base":"stations","main":{
        "temp":284.66,"pressure":1016.88,"humidity":68,"temp_min":284.66,"temp_max":284.66,"sea_level":1024.55,"grnd_level":1016.88},"wind":{
        "speed":4.91,"deg":97.501},"clouds":{
        "all":92},"dt":1476377768,"sys":{
        "message":0.1769,"country":"GB","sunrise":1476339772,"sunset":1476378552},"id":2643743,"name":"London","cod":200

}
因此,当您对上述获得的json字符串执行
json\u decode()
时,输出如下

array (
  'coord' => 
  array (
    'lon' => -0.13000000000000000444089209850062616169452667236328125,
    'lat' => 51.50999999999999801048033987171947956085205078125,
  ),
  'weather' => 
  array (
    0 => 
    array (
      'id' => 804,
      'main' => 'Clouds',
      'description' => 'overcast clouds',
      'icon' => '04d',
    ),
  ),
  'base' => 'stations',
  'main' => 
  array (
    'temp' => 284.66000000000002501110429875552654266357421875,
    'pressure' => 1016.8799999999999954525264911353588104248046875,
    'humidity' => 68,
    'temp_min' => 284.66000000000002501110429875552654266357421875,
    'temp_max' => 284.66000000000002501110429875552654266357421875,
    'sea_level' => 1024.549999999999954525264911353588104248046875,
    'grnd_level' => 1016.8799999999999954525264911353588104248046875,
  ),
  'wind' => 
  array (
    'speed' => 4.910000000000000142108547152020037174224853515625,
    'deg' => 97.501000000000004774847184307873249053955078125,
  ),
  'clouds' => 
  array (
    'all' => 92,
  ),
  'dt' => 1476377768,
  'sys' => 
  array (
    'message' => 0.176900000000000001687538997430237941443920135498046875,
    'country' => 'GB',
    'sunrise' => 1476339772,
    'sunset' => 1476378552,
  ),
  'id' => 2643743,
  'name' => 'London',
  'cod' => 200,
)
因此,在获得数组后,它似乎是一个
单个对象
,其中包含一些已获得的密钥的数组

因此,您只能从使用名为
foreach()
的循环运算符获得的输出中获取数据。因此,通过使用
foreach()
数组数据将以单一方式进入循环,然后它将操纵结果。

以下是结果

<?php
 $url     = "http://api.openweathermap.org/data/2.5/weather?q=London,uk&units=imperial&cnt=1&lang=en&appid=YOURAPI";
 $json    = file_get_contents($url);
 $data    = json_decode($json, true);
 echo $temp = $data['main']['temp'];
?>


从命令行运行良好的CURL返回
{“cod”:401,“消息”:“无效的API密钥。请参阅http://openweathermap.org/faq#error401 更多信息。“
当浏览器返回响应时,他们可能会更改连接要求,或者当不在浏览器中使用时,您的按键达到了某些限制。(此外,如果该密钥是特定于您的密钥,则应修改/删除该密钥)@欧亚大陆。我已经提供了结果。检查一下,让我知道你的期望。感谢你的这些步骤。非常感激这是什么?答案应该有一个解释。你遇到了一个问题。这就是为什么我为你提供了代码。试试吧。你需要解释什么是错误的,以及你的代码是如何修复的。这应该是一个e教育网站,不仅仅是代码片段的存储库。我不知道…我的浏览器可能会出现问题。我收到警告:文件内容(?q=London,uk&appid=a1fc2c19d548237a56e0edd7b79b3ebc):无法打开流:HTTP请求失败!HTTP/1.1 400错误请求位于/home/pray4mal/public_html/pray dev/weather/weather.php第27行on line$json=file_get_contents($url);您可以在浏览器中访问它吗尝试在浏览器中点击它“它正在工作,但数据似乎错误284.66..我正在向api.openweathermap寻求帮助