3

here is my PHP script.

do2:locu alexus$ cat venuesearch.php 
<?php

$KEY='XXXXXXXXXXXXXXX';
$URL='http://api.locu.com/v1_0/venue/search/?api_key=';

$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $URL.$KEY);
curl_setopt($ch, CURLOPT_HEADER,0);

print_r(json_decode(curl_exec($ch),TRUE));

?>
do2:locu alexus$ 

locu service provides output in JSON format. When I run script I'm getting output all in long single line.

sample of output:

do2:locu alexus$ php venuesearch.php 
{"meta": {"cache-expiry": 3600, "limit": 25}, "objects": [{"categories": ["restaurant"], "country": "United States",.......... 

What am I missing? How can I access each of those variables? maybe it makes sense to convert it into XML?

* UPDATE * : .. in example #1 of PHP: json_decode - Manual shows formated output, if I use true then I get array, I'm not getting neither formatet output nor array.

6
  • im not sure where you would like to access it. Commented Feb 24, 2013 at 23:12
  • whats wrong with output JSON format into one long single line? Commented Feb 24, 2013 at 23:12
  • You can access it through: $var = json_decode(); $var['key'] eg. Commented Feb 24, 2013 at 23:12
  • When you parse the JSON in PHP, you get back a PHP array or object and can do whatever you want to do with it. Doesn't php.net/manual/en/function.json-decode.php explain it? But the output you posted shouldn't be the result of json_decode. Are you sure you are not printing the response directly? And no, it does not make sense to convert a data format into another one in this case, you just have to parse the data properly. Commented Feb 24, 2013 at 23:13
  • > "maybe it makes sense to convert it into XML" - heh, then you've got 2 problems. Commented Feb 24, 2013 at 23:13

2 Answers 2

3

Try adding:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

before execution.

It looks like the execution is simply printing the response rather than returning it as a string to be processed by json_decode.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh, I missed that. Makes more sense than my answer :) The tricky thing is that print_r(null) outputs nothing. A good reason to use var_dump instead...
@fab Your answer is still good because separating each procedure can help to identify the probelm. alexus, I would still take a look at fab's answer to isolate the problem because it's far easier to debug each individual function call rather than a bunch of nested calls.
2

You should look at the original data:

$json = curl_exec($ch);
var_dump($json);

Your described output is only possible if the API returns a json encoded json string, like that:

"{\"meta\": {\"cache-expiry\": 3600, \"limit\": 25}, \"objects\": [{\"categories\": [\"restaurant\"], \"country\": \"United States\",.......... '

(note the outer quotes, they are part of the string)

This is very weird and definitly a bug in the API but the only way to get around it is to decode it twice:

$data = json_decode(json_decode($json));

Edit: Forget that, Stegrex has figured it out.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.