tags:

views:

466

answers:

3

for some reason I can't get the information out of a returned jsonp string,

<?php
// Created by Talisman 01/2010 ★✩ 

$vorto = $_GET['vorto']; // Get the Word from Outer Space and Search for it!

if (isset($vorto))
 {
 echo $vorto;
 } else {
  $Help = "No Vorto -> add ?vorto=TheWordYouWant to the end of this website";
  echo $Help;
 }



// Now Lets Search Alex's Vortaro, It uses jsonp
// ex. http://vortaro.us.to/ajax/epo/eng/petas/?callback=?
// Future Feature inproved language functinality

$AVurl1 = "http://vortaro.us.to/ajax/epo/eng/"; 
$AVurl2 = "/?callback=";
$AVfinalurl= $AVurl1 . $vorto . $AVurl2;

echo $AVfinalurl . ' </br> '; // DEBUG CODE 

$AVcontent = file_get_contents($AVfinalurl) ;
echo $AVcontent . ' </br> ';   // DEBUG CODE 

// ★✩ Why does this next line not work?

 $AVDecode = json_decode($AVcontent);


// /* 
  if(isset( $AVcontent)) {          // DEBUG CODE
  echo "json_decode set AVcontent" . ' </br> ';
  } else {
  echo "something fishy here" . ' </br> ';
  }

 if (empty($AVcontent)){
  echo "EMPTY EMPTY" . ' </br> ';
  } else {
  echo "Not Empty". ' </br> ';
  }

echo $AVDecode . ' </br> ';
// */

// Why can't I echo or access information with $AVDecode? Is it something with
// jsonp?

?>

this is my results

komputilojhttp://vortaro.us.to/ajax/epo/eng/komputiloj/?callback=

({"text":"komputilo: computer"})

json_decode set AVcontent

Not Empty

I should be seeing the echo $AVDecode

+3  A: 

Debugging suggestion:

Check the output of json_last_error(). It should give you an exact reason why it doesn't work. Available from PHP 5.3.0 only, though.

The reason:

JSONP is not identical with JSON. It contains extra data that breaks json_decode().

Solution:

Remove the extra brackets using substr($AVDecode, 1, strlen($AVDecode)-2)

Pekka
I got this Fatal error: Call to undefined function json_last_error() in /Users/briancarpenter/Sites/Vortoj/preniVortoj.php on line 53 with echo json_last_error()
Klanestro
Oh, its > 5.3.0 only. You probably have an older PHP version. Hang on, I'll run it. Can you point out exaclty what you get from the data source?
Pekka
Decoding: ({"text":"<b>peti</b>: ask, ask for, beg, bid, request"}) works for me *if I remove the surrounding brackets* `()`. json_decode can't handle jsonp. You need to either remove the brackets manually, or (better) make your data source output proper json.
Pekka
Its a dictionary Esperanto to English translation, can I search for and remove these () there has got to be a way. Another site uses it like this http://libraro.co.cc/
Klanestro
+1. JSONP is JSON with a function wrapper around it. You could remove the function wrapper using the substr function, then evaluate the remaining JSON statement.
Andy E
@Klanestro: Yes, it's possible no problem. Check out http://stackoverflow.com/questions/2093420/remove-and-tags-from-an-array-in-php/2093452#2093452 and replace `<>` by `()`.
Pekka
Or use substr() as Andy E suggests. It's fine in this case.
Pekka
You need to clean every JSONP request you make. You can use `substr($AVDecode, 1, strlen($AVDecode)-2)`
Pekka
A: 

you cant echo an object or an array. please tell us what this line prints out:

print_r(json_decode($AVcontent));

place it right after the $AVDecode = json_decode($AVcontent);

antpaw
Absolutly nothing happens, no change print_r(json_decode($AVcontent));does nothing,
Klanestro
maybe thats ({"text":"komputilo: computer"}) an invalid json string http://www.jsonlint.com/
antpaw
no komputilo is the $vorto = $_GET['vorto'] that the page starts out with.
Klanestro
A: 

Your example URL returns

?({"text":"<b>peti</b>: ask, ask for, beg, bid, request"})

JSONP is not valid JSON, it will wrap it into your supplied callback like

callbackname(JSONIsInHere)

So you need to substring $AVcontent from the first occurrence of ( to the the last ocurrence of ) so that you will get the callback parameter which is valid JSON and can be encoded with json_decode.

Daff