Solved

WAMP Server PHP and JSON Decode

Posted on 2015-02-04
5
630 Views
Last Modified: 2015-02-05
Hi,

Hoping someone has a solutions to this.

We have a page which pulls some json information from an api and then we use a php file to process the decode. I have getting the JSON but nothing happens on the decode.  We are running WAMP (its a local install) and PHP json is enabled.

JSON returned is working.. I can confirm the script is working on Mac OSX and Linux jut not windows :/

{"Result":"Success","Orders":{"table":"T33","Starter":{"has_items":0},"Main":{"has_items":1,"0":{"prd_name":"Food","prd_qty":"1","note":"","has_options":1,"options":[{"option":"Steak Cooked","value":"Medium"},{"option":"Sause","value":"Pepper"}]}},"Desert":{"has_items":1,"0":{"prd_name":"Desert","prd_qty":"1","note":"","has_options":0}}}}

Open in new window

0
Comment
Question by:Benji_
5 Comments
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 40590065
can you please show the php code?
0
 
LVL 9

Expert Comment

by:Brian Tao
ID: 40590375
I have WAMPSERVER 2.2 and it works with the following code:
<?php
$jsonStr = '{"Result":"Success","Orders":{"table":"T33","Starter":{"has_items":0},"Main":{"has_items":1,"0":{"prd_name":"Food","prd_qty":"1","note":"","has_options":1,"options":[{"option":"Steak Cooked","value":"Medium"},{"option":"Sause","value":"Pepper"}]}},"Desert":{"has_items":1,"0":{"prd_name":"Desert","prd_qty":"1","note":"","has_options":0}}}}';
$result = json_decode($jsonStr);
echo "<pre>";
print_r($result);
echo "</pre>";
?>

Open in new window


So I'm guessing it's the part
a page which pulls some json information from an api
not working.  Can you provide your PHP code?
0
 
LVL 2

Author Comment

by:Benji_
ID: 40591079
Hello,

Code is

$curl_url = "http://192.168.1.15/" . 'api/kitchen/getKitchenOrders';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$buffer = curl_exec($ch);
curl_close($ch);

$json = json_decode($buffer, true);

Open in new window

0
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 40591628
I'd suggest dumping the $buffer to a file immediately before you decode it and logging the result to the same file:

file_put_contents("buffer.dump","BUFFER = " . $buffer . "\n",FILE_APPEND | LOCK_EX);
$json = json_decode($buffer,true);
file_put_contents("buffer.dump","DECODED = " . print_r($json,true) . "\n",FILE_APPEND | LOCK_EX);

Post the resulting buffer.dump file here as an attachment.

I'd be willing to bet there's some unexpected content in the $buffer. You don't have the headers option set, according to your code, so that's probably not it (although you can explicitly disable them with CURLOPT_HEADER set to false), but there may be something else going on.
0
 
LVL 2

Author Comment

by:Benji_
ID: 40591971
Hi,

Thanks for the dumping idea it was dumping a random full stop from somewhere in the file before sending it which didn't copy when I coped it from the file directly.

its now all working :)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now