【问题标题】:How to load Json Array with php Curl or file_get_contents()?如何使用 php Curl 或 file_get_contents() 加载 Json 数组?
【发布时间】:2018-09-25 01:14:51
【问题描述】:

我正在尝试从此 URL 加载 Json 数组

到目前为止我尝试过的如下:

//By Using File_get_contents()

$insta_source = file_get_contents($url);
$insta_array = json_decode($insta_source, TRUE);    
var_dump($insta_array);

//AND
//By Using php CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$server_output = curl_exec ($ch);
curl_close ($ch);
var_dump($server_output);

//As in output it gives "Bool(false)" (get_file_contents())
//Curl Error gives : "bool(true)" but not the json array

非常感谢, 吉姆

【问题讨论】:

  • 你读过错误吗? 您无权查看此页面。
  • 感谢您回来。我更新了我的问题,请查看。
  • 我什至尝试过发布请求,它给出了 403 错误代码。你对此无能为力。
  • 请再次检查我的问题,我已更新。在 CURL 中,它在添加其他参数后给了我“Bool(true)”。现在的问题是我们需要 Json Array 而不是 Bool
  • curl_close($ch);之前添加$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);,看看响应码是什么。它是 403。您无法访问该页面。

标签: php file-get-contents php-curl


【解决方案1】:

如果您想要 cURL 请求的内容,您必须使用 CURLOPT_RETURNTRANSFER 设置,如 http://www.php.net/curl_exec 所述:

成功返回TRUE,失败返回FALSE。但是,如果设置了CURLOPT_RETURNTRANSFER 选项,它将在成功时返回结果,在失败时返回FALSE

所以你只需要启用CURLOPT_RETURNTRANSFER 设置。

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

【讨论】:

    猜你喜欢
    • 2012-03-20
    • 2012-10-11
    • 1970-01-01
    • 2013-11-17
    • 2014-09-23
    • 1970-01-01
    • 2012-06-19
    • 2011-06-23
    • 2013-07-23
    相关资源
    最近更新 更多