EmperorJazzy Posted June 22, 2011 Share Posted June 22, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/240103-json-reading-of-file-errors/ Share on other sites More sharing options...
salathe Posted June 22, 2011 Share Posted June 22, 2011 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)); Quote Link to comment https://forums.phpfreaks.com/topic/240103-json-reading-of-file-errors/#findComment-1233309 Share on other sites More sharing options...
EmperorJazzy Posted June 23, 2011 Author Share Posted June 23, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/240103-json-reading-of-file-errors/#findComment-1233630 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.