2

由于某种原因,我无法从返回的 jsonp 字符串中获取信息,

<?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 

// ★✩ 为什么下一行不起作用?

 $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?

?>

这是我的结果

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

({"text":"komputilo: 计算机"})

json_decode 设置AV内容

不是空的

我应该看到 echo $AVDecode

4

3 回答 3

6

调试建议:

检查json_last_error()的输出。它应该给你一个它不起作用的确切原因。不过,仅适用于 PHP 5.3.0。

原因:

JSONPJSON不同。它包含破坏 json_decode() 的额外数据。

解决方案:

使用删除多余的括号substr($AVDecode, 1, strlen($AVDecode)-2)

于 2010-01-20T12:37:15.867 回答
0

你不能回显一个对象或一个数组。请告诉我们这一行打印出来的内容:

print_r(json_decode($AVcontent));

把它放在后面 $AVDecode = json_decode($AVcontent);

于 2010-01-20T12:40:20.553 回答
0

您的示例网址返回

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

JSONP 不是有效的 JSON,它会将其包装到您提供的回调中,例如

callbackname(JSONIsInHere)

因此,您需要将 $AVcontent 从第一次出现 ( 到最后一次出现 ) 进行子串化,这样您将获得回调参数,该参数是有效的 JSON 并且可以使用 json_decode 进行编码。

于 2010-01-20T12:56:33.120 回答