【问题标题】:php file_get_contents not working on serverphp file_get_contents 在服务器上不起作用
【发布时间】:2014-09-19 09:10:41
【问题描述】:

我在使用file_get_contents时遇到问题

我需要从 url 获取内容

$header=array();
$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";
$header[] = "Pragma: ";
$opts = array(
                'http'=>array(
                            'method'=>"GET",
                            'header'=>implode('\r\n',$header),
                            'proxy' => 'proxy_address',
                            'request_fulluri' => True
                            )
                    );

$context = stream_context_create($opts);
$file = file_get_contents($url, false, $context);
echo $file;

错误信息说:file_get_contents(my url) [function.file-get-contents]: failed to open stream: Connection timed out

我尝试了curl 请求,但得到了相同的响应。我怎么解决这个问题?

【问题讨论】:

  • 您的托管公司可能会阻止传出请求,请与他们核实。
  • 你在各种主机上试过代码了吗?看起来不错,我唯一能想到的是您的主机/外部站点没有响应。
  • 其他网站代码工作正常,我认为我尝试解析的网站阻止了我的请求(这就是我添加代理的原因)但它没有帮助:(

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


【解决方案1】:

尝试将超时设置添加到您的选择中:

$opts = array(
                'http'=>array(
                            'method'=>"GET",
                            'header'=>implode('\r\n',$header),
                            'proxy' => 'proxy_address',
                            'request_fulluri' => True
                            ),
                'timeout' => 60
);

此示例将连接超时设置为 60 秒

【讨论】:

    猜你喜欢
    • 2013-10-02
    • 1970-01-01
    • 2012-11-15
    • 1970-01-01
    • 2014-06-23
    • 2018-01-11
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多