jsonp-> json_decode() [英] jsonp -> json_decode()

查看:100
本文介绍了jsonp-> json_decode()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

?>

这是我的结果

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

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

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

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

json_decode设置AVcontent

json_decode set AVcontent

不为空

我应该看到回显$ AVDecode

I should be seeing the echo $AVDecode

推荐答案

调试建议:

检查 json_last_error()<的输出 .它应该给您确切的原因为什么它不起作用.不过,仅从PHP 5.3.0起可用.

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.

原因:

JSONP

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

解决方案:

使用substr($AVDecode, 1, strlen($AVDecode)-2)

这篇关于jsonp-&gt; json_decode()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆