【问题标题】:file_get_contents request to external site对外部站点的 file_get_contents 请求
【发布时间】:2016-03-27 14:40:54
【问题描述】:

我正在尝试对这个 Demo URL 执行 file_get_contents
但是,服务器无法从外部站点获取数据。如果我回显 file_get_contents,这是我得到的错误:

发现文档已移动 here。 Apache/2.4 服务器位于 spotifycharts.com 端口 80

我已在 php.ini file 中打开 register_global,但这没有帮助。

为了确保我的网站能够从外部网站获取数据,最合乎逻辑的检查是什么?

【问题讨论】:

标签: php json apache http file-get-contents


【解决方案1】:

【讨论】:

  • 简单!有用。必须等待 5 分钟才能接受答案
【解决方案2】:

您可能需要使用 cURL 进行请求,我认为 file_get_contents() 不能跟随 302 重定向。

这样的东西应该可以工作......

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$a = curl_exec($ch);
if (preg_match('#Location: (.*)#', $a, $r))
  $l = trim($r[1]);

How to get the real URL after file_get_contents if redirection happens?
Source

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 2016-11-25
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 2012-12-10
    • 1970-01-01
    相关资源
    最近更新 更多