doufu9145 2015-07-10 12:15 采纳率: 0%
浏览 227

API数据返回HTML

I have added new data to my API. I want to return it as plain text

This is the API response PHP returns.

 {
    "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" 
             }
      }

I want to return the pressure value on my html page so my users can see the reading. I am having issues displaying it.

This is my 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" } }';
}
  • 写回答

2条回答 默认 最新

  • douyanlu7380 2015-07-10 12:26
    关注

    In order to fetch data from a JSON source you should parse the data with the json_decode() method. You can then use the second parameter to parse it into an array. If you omit the second parameter you would get an array of objects.

    Important: It seems your JSON has a syntax error too. I have added a weather key before the weather information.

    $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);
    

    You should then be able to fetch the pressure as an associative array.

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

    Hope this can help you, happy coding!

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败