dsbj66959 2010-01-20 12:33
浏览 13
已采纳

jsonp - > json_decode()

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条回答 默认 最新

  • duande9301 2010-01-20 12:37
    关注

    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)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接
  • ¥15 MATLAB中使用parfor,矩阵Removal的有效索引在parfor循环中受限制
  • ¥20 Win 10 LTSC 1809版本如何无损提升到20H1版本
  • ¥50 win10 LTSC 虚拟键盘不弹出
  • ¥30 微信小程序请求失败,网页能正常带锁访问
  • ¥15 Matlab求解微分方程,如何用fish2d进行预优?