pears456
pears456

Reputation: 13

PHP Decoding JSON - Null Output

I have probably spent all day trying to figure this out. I have read multiple questions here on stack and also have been reading articles and checking on documentation, but I can't seem to figure out why this batch of code just produces a null output. Am I missing brackets, calling something wrong, ect?

<?php
$url = "http://api.openweathermap.org/data/2.5/weather?id=4879890&units=imperial";
$str = file_get_contents($url);
$json = json_decode($str);
$temp =  $json['main']['temp_min'];
$content = $temp;


$array = array(
     "content" => $content,
      refresh_requency => 30

);


echo json_encode($array);
?>

Again what I'm asking is can someone point out to me or tell me what I'm doing wrong. Is it my server that's just not handling the data correctly? That could be a possibility.

One other thing I've tried is to just print out $temp and/or the other variable like $str. When I do that though they don't even show up so that's what I think my problem is just not sure how to fix it.

Update

I've come to the conclusion that it's my web hosting service. As if I add var_dump($json) I get a null output null output.

Also to confirm that its my webhost if I run error_reporting(E_ALL); ini_set('display_errors', 1); it points to the file php.ini not allowing outgoing connections. I edited that same file on my local home server(raspberry pi) ran the same file and it works fine.

Upvotes: 0

Views: 348

Answers (5)

Vidya L
Vidya L

Reputation: 2314

Access $temp like this

$temp =  $json->main->temp_min;

you will get the desired output.

Also, you need to allow allow_url_fopen in your php.ini config file. Some hosts disallow it for security reasons

Upvotes: 1

Vivek Shukla
Vivek Shukla

Reputation: 207

Below is the working solution for your above code:

<?php
$url = "http://api.openweathermap.org/data/2.5/weather?id=4879890&units=imperial";
$str = file_get_contents($url);
$json = json_decode($str, true);
$temp =  $json['main']['temp_min'];
$content = $temp;


$array = array(
     "content" => $content,
      "refresh_requency" => 30

);


echo json_encode($array);
?>

When I executed your code, I found 2 problem into your code snippet:

1) You were trying to use object of type stdClass as array.

Solution:

<?php
$url = "http://api.openweathermap.org/data/2.5/weather?id=4879890&units=imperial";
$str = file_get_contents($url);
$json = json_decode($str);
$temp =  $json->main->temp_min;
$content = $temp;


$array = array(
     "content" => $content,
      "refresh_requency" => 30

);


echo json_encode($array);
?>

2) You did not put array key into quotes:

$array = array(
     "content" => $content,
      refresh_requency => 30

);

It should be :

$array = array(
     "content" => $content,
      "refresh_requency" => 30

);

Upvotes: 1

Ecko Santoso
Ecko Santoso

Reputation: 511

Just want help

I change your code like this and that be correctly

<?php
    $url = "http://api.openweathermap.org/data/2.5/weather?id=4879890&units=imperial";
    $str = file_get_contents($url);
    $json = json_decode($str, TRUE);    // Wrong here
    $temp =  $json['main']['temp_min'];
    $content = $temp;

    $array = array(
       "content" => $content,
       "refresh_requency" => 30         // And wrong here, it's must string
    );

    echo json_encode($array);
 ?>

More information about json decode in php to array or object http://php.net/manual/en/function.json-decode.php

Upvotes: 0

haris
haris

Reputation: 3875

The second parameter of json_decode() is assoc (associative). By default it is 0. When it is 0 (default) the json_decode() will return an object, not an array. That's why you are unable to access temp_min by using $json['main']['temp_min'];

However If you use with the value 1 as second parameter, the function will return an array. Parameter 1 means setting associative to 1 (true). So use $json = json_decode($str, true); instead of $json = json_decode($str);. You will be able to access with $json['main']['temp_min']; now.

Also you forgot double quote on line 13 (refresh_requency). Goodluck.

<?php

$url = "http://api.openweathermap.org/data/2.5/weather?id=4879890&units=imperial";
$str = file_get_contents($url);
$json = json_decode($str, true);

$temp =  $json['main']['temp_min'];
$content = $temp;


$array = array(
    "content" => $content,
    "refresh_requency" => 30
);

echo json_encode($array);

Upvotes: 0

Cheery
Cheery

Reputation: 16214

$json = json_decode($str, true);

You need second argument to convert json string into associative array instead of the object. And you are trying to use array ($json['main']['temp_min']), not object. Also

$array = array(
     "content" => $content,
     "refresh_requency" => 30
);

The code looks like

<?php
$url = "http://api.openweathermap.org/data/2.5/weather?id=4879890&units=imperial";
$str = file_get_contents($url);
$json = json_decode($str, true);
$content = $json['main']['temp_min'];

$array = array(
 "content" => $content,
 "refresh_requency" => 30
);

echo json_encode($array);

And result is http://codepad.viper-7.com/euBNAk :

{"content":44.6,"refresh_requency":30}

Upvotes: 0

Related Questions