Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.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 API数据返回到HTML_Php_Json_Api_Response - Fatal编程技术网

Php API数据返回到HTML

Php API数据返回到HTML,php,json,api,response,Php,Json,Api,Response,我已将新数据添加到API中。我想以纯文本形式返回它 这是PHP返回的API响应 { "apiVersion":"1.0", "data":{ "location":"London",: { "pressure":"1021", "temperature":"23", "skytext":"Sky is Clear",

我已将新数据添加到API中。我想以纯文本形式返回它

这是PHP返回的API响应

 {
    "apiVersion":"1.0", 
    "data":{ 
            "location":"London",:
            { 
             "pressure":"1021",
              "temperature":"23", 
              "skytext":"Sky is Clear",
              "humidity":"40", 
              "wind":"18.36 km/h", 
              "date":"07-10-2015", 
              "day":"Friday" 
             }
      }
我想在我的html页面上返回压力值,这样我的用户就可以看到读数。我在显示它时遇到问题

这是我的PHP api.PHP

require_once('./includes/config.php');
require_once('./includes/functions.php');
error_reporting(0);
header('Content-Type: text/plain; charset=utf-8;');

$city = $_GET['city'];

if(isset($city)) {

    $weather = new Weather($conf['apikey'], $_GET['f']);
    $weather_current = $weather->get($city, 0, 0, null, null);

    $now = $weather->data(0, $weather_current);

    if($now['location'] !== NULL) {
        echo '{"apiVersion":"1.0", "data":{ "location":"'.$now['location'].'", "temperature":"'.$now['temperature'].'", "pressure":"'.$now['pressure'].'", "skytext":"'.$now['description'].'", "humidity":"'.$now['humidity'].'", "wind":"'.$now['windspeed'].'", "date":"'.$now['date'].'", "day":"'.$now['day'].'" } }';
    } else {
        echo '{"apiVersion":"1.0", "data":{ "error":"The \'city\' requested is not available, make sure it\'s a valid city." } }';
    }
} else {
    echo '{"apiVersion":"1.0", "data":{ "error":"You need to specify the city parameter" } }';
}

为了从JSON源获取数据,应该使用方法解析数据。然后可以使用第二个参数将其解析为数组。如果省略第二个参数,将得到一个对象数组

重要提示:您的JSON似乎也有语法错误。我在天气信息之前添加了一个
天气

$data = '{
    "apiVersion":"1.0",
    "data":{
        "location":"London",
        "weather":{              // Notice the new key!
            "pressure":"1021",
            "temperature":"23",
            "skytext":"Sky is Clear",
            "humidity":"40",
            "wind":"18.36 km/h",
            "date":"07-10-2015",
            "day":"Friday"
        }
    }
}';

$json = json_decode($data, true);
然后,您应该能够以关联数组的形式获取压力

$pressure = $json['data']['weather']['pressure']; // Equals: 1021

希望这能帮助你,快乐编码

首先,您需要验证JSON。它缺少一些关键的东西,这些东西会让你无法解析它。用于验证JSON

修改JSON使其有效后,我执行了以下操作:

$json = '{"apiVersion":"1.0", "data":{ "location":"London", "data":{ "pressure":"1021", "temperature":"23", "skytext":"Sky is Clear", "humidity":"40", "wind":"18.36 km/h", "date":"07-10-2015", "day":"Friday" }}}';

$obj_style = json_decode($json);
$array_style = json_decode($json, true);

echo $obj_style->data->data->pressure;
echo $array_style['data']['data']['pressure'];
通过使用,我能够设置一种方法,以两种方式解析JSON,一种是作为对象,另一种是作为数组(添加
true
标志以数组的形式返回结果)


从这里开始,你所要做的就是把城市钻到你想显示的信息中。

{“apiVersion”:“1.0”,“数据”:{“位置”:“伦敦”:{“压力”:“1021”,“温度”:“23”,“天空文字”:“天空晴朗”,“湿度”:“40”,“风”:“18.36公里/小时”,“日期”:“2015年10月7日”,“日期”:“星期五”}
如何显示json结果?我仍在尝试调试此问题,但需要一些帮助来解决问题。如果您可以提供更多上下文,将更容易帮助您。到目前为止,我们没有什么需要处理的。欢迎使用Stack Overflow!这个问题的信息有点少。您可以分享您尝试过的内容以及遇到的问题吗你遇到了什么?你现在展示的是JSON(在这一点上是无效的),而不是API。