Solved

WAMP Server PHP and JSON Decode

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

Give your grad a cloud of their own!

With up to 8TB of storage, give your favorite graduate their own personal cloud to centralize all their photos, videos and music in one safe place. They can save, sync and share all their stuff, and automatic photo backup helps free up space on their smartphone and tablet.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to create a scatter graph with straight lines 6 27
Problem with Simple PHP/mySQL Query 3 51
PHP_POST() error message 9 42
unset shopping cart session 15 32
Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

920 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

14 Experts available now in Live!

Get 1:1 Help Now