php file_get_contents无法在服务器上运行

时间:2014-09-19 09:10:42

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

使用file_get_contents

时出现问题

我需要从网址获取内容

$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请求,但得到的回复相同。我怎么解决这个问题?

1 个答案:

答案 0 :(得分:0)

尝试为您的选项添加超时设置:

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

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