在进行后端开发的时候,我们经常需要对接一些第三方接口获取数据,比如获取天气预报数据、经纬度信息、对接微信支付宝等接口,使用到最多的方法之一就是curl了。理想的情况当然是对方立即处理了我们的请求并返回数据,但是如果对方服务器一直没有响应,我们的程序又被阻塞着不能继续进行后续处理,这样我们的服务器也很可能被拖垮,所以这个问题不能不考虑。
cURL 是我们常用的一种比较靠谱的访问HTTP协议接口的lib库,性能高,还有一些并发支持的功能等。
cURL: curl_setopt($ch, opt) 可以设置一些超时的设置,主要包括:
- CURLOPT_TIMEOUT 设置cURL允许执行的最长秒数。
- CURLOPT_TIMEOUT_MS 设置cURL允许执行的最长毫秒数。// (在cURL 7.16.2中被加入。从PHP 5.2.3起可使用。 )
- CURLOPT_CONNECTTIMEOUT 在发起连接前等待的时间,如果设置为0,则无限等待。
- CURLOPT_CONNECTTIMEOUT_MS 尝试连接等待的时间,以毫秒为单位。如果设置为0,则无限等待。 //(在cURL 7.16.2中被加入。从PHP 5.2.3开始可用。)
- CURLOPT_DNS_CACHE_TIMEOUT 设置在内存中保存DNS信息的时间,默认为120秒。
例子:
<?php
/**
* CURL访问超时时间设置
*/
if ($_GET['type'] === 'curl') {
$ch = curl_init('https://www.baidu.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOSIGNAL, 1);//注意,毫秒超时一定要设置这个
// curl_setopt($ch, CURLOPT_TIMEOUT, 1);//普通秒级超时
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 200);//超时毫秒,cURL 7.16.2中被加入。从PHP 5.2.3起可使用
$data = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
curl_close($ch);
if ($curl_errno > 0) {
echo "cURL Error ($curl_errno): $curl_error\n";
} else {
echo "Data received: $data\n";
}
}
流处理方式访问HTTP
除了curl,我们还经常自己使用fsockopen、或者是file操作函数来进行HTTP协议的处理,所以,我们对这块的超时处理也是必须的。一般连接超时可以直接设置,但是流读取超时需要单独处理。
完整案例:
<?php
/**
* CURL访问超时时间设置
*/
if ($_GET['type'] === 'curl') {
$ch = curl_init('https://www.fuor88.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOSIGNAL, 1);//注意,毫秒超时一定要设置这个
// curl_setopt($ch, CURLOPT_TIMEOUT, 1);//普通秒级超时
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 200);//超时毫秒,cURL 7.16.2中被加入。从PHP 5.2.3起可使用
$data = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
curl_close($ch);
if ($curl_errno > 0) {
echo "cURL Error ($curl_errno): $curl_error\n";
} else {
echo "Data received: $data\n";
}
}
/**
* socket超时处理
*/
if($_GET['type'] === 'socket'){
// Timeout in seconds
$timeout = 1;
$fp = fsockopen("example2554343.com", 80, $errno, $errstr, $timeout);
if ($fp) {
fwrite($fp, "GET / HTTP/1.0\r\n");
fwrite($fp, "Host: example.com\r\n");
fwrite($fp, "Connection: Close\r\n\r\n");
stream_set_blocking($fp, true);
//重要,设置为非阻塞模式
stream_set_timeout($fp,$timeout);
//设置超时
$info = stream_get_meta_data($fp);
$data = '';
while ((!feof($fp)) && (!$info['timed_out'])) {
$data .= fgets($fp, 4096);
$info = stream_get_meta_data($fp);
ob_flush();
flush();
}
if ($info['timed_out']) {
echo "Connection Timed Out!";
} else {
echo $data;
}
}
}
/**
* file_get_contents 超时
*/
if($_GET['type'] === 'file_get_contents'){
$timeout = array(
'http' => array(
'timeout' => 1//设置一个超时时间,单位为秒
)
);
$ctx = stream_context_create($timeout);
$text = file_get_contents("http://example.com1/", 0, $ctx);
echo $text;
}
/**
* fopen超时
*/
if($_GET['type'] === 'fopen'){
$timeout = array(
'http' => array(
'timeout' => 1//设置一个超时时间,单位为秒
)
);
$ctx = stream_context_create($timeout);
if ($fp = fopen("http://example.com1/", "r", false, $ctx)) {
while( $c = fread($fp, 8192)) {
echo $c;
}
fclose($fp);
}
}