Jump to content

JSON Reading of File - Errors


EmperorJazzy

Recommended Posts

Evening All,

 

I'm noob to the JSON stuff; and trying to learn (if anyone has a php tutorial I'd be appreciative if you could let me know). I've written some basic code to begin breaking down the process I want to achieve. The following is an extract where I'm simply trying to call the .json output and var_dump'ing it. The output error is below;

 

<body>
<?php

ini_set ("display_errors", "1");
error_reporting(E_ALL);

$jsonFile = file_get_contents('http://api.xxxxxx.com/xxxxxxx.json?client_id=975a48c6c927c42c91cxc2348eafc9766d44892d'); 
$json = json_decode($jsonFile, true);

var_dump(json_decode($json));
var_dump(json_decode($json, true));

?>

</body>

 


Error
[code]

Warning: json_decode() expects parameter 1 to be string, array given in /var/www/vhosts/einterfaces.com.au/httpdocs/testjson/index.php on line 15
NULL 
Warning: json_decode() expects parameter 1 to be string, array given in /var/www/vhosts/einterfaces.com.au/httpdocs/testjson/index.php on line 16
NULL

 

The file itself may contain header information.

 

Expected output:

HTTP/1.1 200 OK
X-Runtime:
139
ETag:
"02023e97fc389c84f74e7b1ed9d9899e"
X-L-COOKIE:
X-RateLimit-Client-RateLimit:
2500
Content-Length:
40060
X-UA-Compatible:
chrome=IE8
Set-Cookie:
_thepoint=e9d7f8167176b1e716644592e770eb09; domain=.xxxxxx.com; path=/; expires=Thu, 23 Jun 2011 10:25:42 GMT; HttpOnly
X-RateLimit-IP-RateResetsAt:
2011-06-22T10:26:42Z
Connection:
close
Server:
nginx/0.7.65
Cache-Control:
private, max-age=0, must-revalidate
X-RateLimit-IP-RateRemaining:
600
X-RateLimit-Client-RateRemaining:
2440
Status:
200
X-RateLimit-IP-RateLimit:
600
X-RateLimit-Client-RateResetsAt:
2011-06-22T10:26:42Z
Date:
Wed, 22 Jun 2011 10:25:42 GMT
Content-Type:
application/json; charset=utf-8

{
   "divisions": [
    {
     "id" : "abbotsford",
     "name" : "Abbotsford",
     "country" : "Canada",
     "timezone" : "Pacific Time (US & Canada)",
     "timezoneOffsetInSeconds" : -28800,
     "lat" : 49.0568,
     "lng" : -122.285,
     "isNowMerchantEnabled" : false,
     "isNowCustomerEnabled" : false,
     "areas": [
           ]
     },
....

 

Again... I am totally noob at this. Thanks in advance for your advice.

Link to comment
https://forums.phpfreaks.com/topic/240103-json-reading-of-file-errors/
Share on other sites

You decode the JSON once, then try to decode the result of decoding twice! Instead, just decode the result from file_get_contents().

 

$jsonFile = file_get_contents('http://api.xxxxxx.com/xxxxxxx.json?client_id=975a48c6c927c42c91cxc2348eafc9766d44892d'); 
var_dump(json_decode($jsonFile));
var_dump(json_decode($jsonFile, true));

Arh, so very true. Always good to have another set of eyes over my code. Thanks!

 

Is any of our wonderful moderators/guru's able to point me in the right direction for a tutorial on PHP and JSON so I can learn without constant posting on the forum. Thanks in advance!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.