0

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" } }';
}
  • `{"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" } }` – Cyclonecode Jul 10 '15 at 12:19
  • how do i show the json result im still trying to debug this just need a little help fixing the issue – user3146197 Jul 10 '15 at 12:22
  • If you can provide more context it would be much easier to help you. As of now we have very little to work with. – AnotherGuy Jul 10 '15 at 12:29
  • Welcome to Stack Overflow! This question is a little short on information. Can you share what you have tried, and what problems you have run into? What you're showing is JSON (non-valid at this point), not an API. – Jay Blanchard Jul 10 '15 at 12:33

2 Answers2

0

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!

AnotherGuy
  • 605
  • 11
  • 20
0

First of all, you need to validate your JSON. It is missing some key things that will keep you from being able to parse it. Use JSONLint to verify your JSON.

After modification the JSON to make it valid I did the following:

$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'];

Using json_decode() I was able to setup a way to parse the JSON two ways, once as an object and once as an array (adding the true flag returns the results as an array).

From there all you have to do is drill town to the bits of information that you want to display.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119