PHP& Json - 返回空白结果,没有错误

时间:2014-11-13 03:10:16

标签: php json

我们过去曾使用过这段代码,我把一个非常有效的例子放在一起,脚本会得到一些json数据并通过wordpress模板呈现。现在它没有正确返回数据,或者此处的Feed出现问题:http://www.testdomain.com/website

<?php
$url = "http://testdomain.com/website/";
$ch = curl_init($url);
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Contet-type: application/json'),
    );

curl_setopt_array($ch, $options);

$json = curl_exec($ch);
$obj = json_decode($json);

?>

感谢任何帮助。

4 个答案:

答案 0 :(得分:3)

您必须将www添加到您的网址,然后再次运行!

$url = "http://www.testdomain.com/website/";

在空白页面上尝试这样:

$url = "http://www.testdomain.com/website/";

$ch = curl_init($url);
$options = array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => array('Contet-type: application/json'),
);

curl_setopt_array($ch, $options);

$json = curl_exec($ch);
$obj = json_decode($json);
var_dump($obj); //<<<< just to see the output

答案 1 :(得分:1)

当某些问题出现时,Json php函数不会触发错误或警告。

大多数情况下,它与在解码或编码时编码或解析JSON的问题有关

使用json_last_error()检查错误。

答案 2 :(得分:1)

请使用 的error_reporting(E_ALL);

无论如何,这似乎有效

<?php
error_reporting(E_ALL);
$obj=json_decode(file_get_contents("http://example.com/website/"));
var_dump($obj);

(虽然,这种方法有一些上升和一些下降,但如果allow_url_fopen为0则它不会起作用,它也可能比卷曲慢,因为它没有#&#'s&#39; 39; t给出一个关于content-length:标题的东西,它等待连接关闭,我认为..在正面,即使没有安装php5-curl也能正常工作)

答案 3 :(得分:1)

感谢@ michael的回答,我明白了。 似乎http://testdomain.com/website/重定向到http://www.testdomain.com/website/ ,默认情况下,curl不遵循重定向,添加

CURLOPT_FOLLOWLOCATION=>true

$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Contet-type: application/json'),
CURLOPT_FOLLOWLOCATION=>true
);

旁注,似乎是Contet-type:application / json header没什么区别..

相关问题