在跨服务器提交的时候 会遇到超时的情况 用context中的timeout时间来控制
错误超过3次或5次后 确实的认为无法连接服务器而彻底放弃 是个好办法 应该值得推荐使用
只要支持context的都应该加上 避免超时浪费时间 这样可以被支持的函数大致有
fsocketopen
fopen
file
curl(curl有自已的变量 CURLOPT_TIMEOUT)等
// 超时时间 单位秒
function wp_file_get_contents($url,$second=6){
$context = stream_context_create(array('http' => array('timeout' =>$second) ));
return file_get_contents($url, 0, $context);
}
//use curl ,replace file_get_contents
function curl_request($url,$timeout=5,$header=array(),$http='http:'){
if (!function_exists('curl_init')) {
throw new Exception('server not install curl');
}
if(stripos($url,'//') === 0){ $url = $http.$url;}
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
if (!empty($header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
$data = curl_exec($ch);
$t = explode("rnrn", $data);
if(count($t)>=2){
list($header, $data) = $t;
}
$info = curl_getinfo($ch);
if ($info['http_code'] == 301 || $info['http_code'] == 302) {
$url = $info['redirect_url'];
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
$data = curl_exec($ch);
}
if(empty($data)){
curl_close($ch);
return false;
}
@curl_close($ch);
return $data;
}
增加超时的时间限制
这里需要注意:set_time_limit只是设置你的PHP程序的超时时间 而不是file_get_contents函数读取URL的超时时间
$opts = array(
'http'=>array(
'method'=>"GET",
'timeout'=>1
)
);
$cnt=0;
while($cnt<3 && ($bb=file_get_contents("http://www.jb51.net", false, stream_context_create($opts)))===FALSE) $cnt++;
山寨版的post传值函数
function Post($url, $post = null){
$context = array ();
if (is_array ( $post )) {
ksort ( $post );
$context ['http'] = array (
'timeout' => 60,
'method' => 'POST',
'content' => http_build_query( $post, '', '&' )
);
}
return file_get_contents ( $url, false, stream_context_create ( $context ) );
}
$data = array (
'name' => 'test',
'email' => 'admin@admin.com',
'submit' => 'submit',
);
错误超过3次或5次后 确实的认为无法连接服务器而彻底放弃 是个好办法 应该值得推荐使用
只要支持context的都应该加上 避免超时浪费时间 这样可以被支持的函数大致有
fsocketopen
fopen
file
curl(curl有自已的变量 CURLOPT_TIMEOUT)等
// 超时时间 单位秒
function wp_file_get_contents($url,$second=6){
$context = stream_context_create(array('http' => array('timeout' =>$second) ));
return file_get_contents($url, 0, $context);
}
//use curl ,replace file_get_contents
function curl_request($url,$timeout=5,$header=array(),$http='http:'){
if (!function_exists('curl_init')) {
throw new Exception('server not install curl');
}
if(stripos($url,'//') === 0){ $url = $http.$url;}
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
if (!empty($header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
$data = curl_exec($ch);
$t = explode("rnrn", $data);
if(count($t)>=2){
list($header, $data) = $t;
}
$info = curl_getinfo($ch);
if ($info['http_code'] == 301 || $info['http_code'] == 302) {
$url = $info['redirect_url'];
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
$data = curl_exec($ch);
}
if(empty($data)){
curl_close($ch);
return false;
}
@curl_close($ch);
return $data;
}
增加超时的时间限制
这里需要注意:set_time_limit只是设置你的PHP程序的超时时间 而不是file_get_contents函数读取URL的超时时间
$opts = array(
'http'=>array(
'method'=>"GET",
'timeout'=>1
)
);
$cnt=0;
while($cnt<3 && ($bb=file_get_contents("http://www.jb51.net", false, stream_context_create($opts)))===FALSE) $cnt++;
山寨版的post传值函数
function Post($url, $post = null){
$context = array ();
if (is_array ( $post )) {
ksort ( $post );
$context ['http'] = array (
'timeout' => 60,
'method' => 'POST',
'content' => http_build_query( $post, '', '&' )
);
}
return file_get_contents ( $url, false, stream_context_create ( $context ) );
}
$data = array (
'name' => 'test',
'email' => 'admin@admin.com',
'submit' => 'submit',
);
尊贵的董事大人
英文标题不为空时 视为本栏投稿
需要关键字 描述 英文标题