Solved

WAMP Server PHP and JSON Decode

Posted on 2015-02-04
5
884 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_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 43

Expert Comment

by:Rob
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 35

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

Create the perfect environment for any meeting

You might have a modern environment with all sorts of high-tech equipment, but what makes it worthwhile is how you seamlessly bring together the presentation with audio, video and lighting. The ATEN Control System provides integrated control and system automation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

690 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