?
Solved

WAMP Server PHP and JSON Decode

Posted on 2015-02-04
5
Medium Priority
?
950 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 2000 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 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…
Suggested Courses

770 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