【发布时间】:2015-09-29 04:32:42
【问题描述】:
我已将新数据添加到我的 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" } }';
}
【问题讨论】:
-
{"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" } } -
我如何显示 json 结果我仍在尝试调试这只是需要一些帮助来解决问题
-
如果你能提供更多的上下文,帮助你会容易得多。到目前为止,我们几乎没有什么可合作的。
-
欢迎来到 Stack Overflow!这个问题的信息有点少。你能分享你尝试过的东西,以及你遇到了什么问题吗?您显示的是 JSON(此时无效),而不是 API。