在用file_get_contents函数的时候,我写了如下的例子:
文件名字:test.php
<?
$test = file_get_contents('http://www.tudou.com/playlist/playindex.do?lid=8294366');
echo $test;
?>当我重复刷新test.php页面的时候。有时候能读到这个视频的内容。但是有的时候提示错误:
Warning: file_get_contents(http://www.tudou.com/playlist/playindex.do?lid=8294366) [function.file-get-contents]: failed to open stream: HTTP request failed!
文件名字:test.php
<?
$test = file_get_contents('http://www.tudou.com/playlist/playindex.do?lid=8294366');
echo $test;
?>当我重复刷新test.php页面的时候。有时候能读到这个视频的内容。但是有的时候提示错误:
Warning: file_get_contents(http://www.tudou.com/playlist/playindex.do?lid=8294366) [function.file-get-contents]: failed to open stream: HTTP request failed!
<?php
/*设置超时配合失败之后尝试多次读取*/
$opts = array(
'http'=>array(
'method'=>"GET",
'timeout'=>1, //设置超时
)
);
$context = stream_context_create($opts);
$cnt=1; //最多尝试10次读取
while(($contents = @file_get_contents("$url",false,$context)) === false && $cnt <= 10 )
$cnt++;
?>
超时和重试,这是网络传输的常用方法