Solved

WAMP Server PHP and JSON Decode

Posted on 2015-02-04
5
706 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

786 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