HTTP访问基础与超时必要性
【HTTP访问】
在PHP环境中,发起HTTP请求通常依赖curl、socket扩展或file_get_contents()等内置函数。
当目标服务器长时间未返回响应时,请求线程会持续阻塞,可能导致服务器进程耗尽甚至崩溃,因此在发起HTTP请求时必须合理配置超时限制。
[ CURL 访问HTTP]
CURL是PHP中广泛使用的HTTP协议客户端库,具备高性能特点,并支持并发请求处理。
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秒。
curl普通秒级超时:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60); //只需设置一个秒数即可
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);
curl普通秒级超时使用:
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
若需进行毫秒级超时控制,需额外增加配置:
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
或者是:
curl_setopt ( $ch, CURLOPT_NOSIGNAL, true); 开启后可支持毫秒级别超时设置
curl一个毫秒级超时的例子:
<?php
if (!isset($_GET['foo'])) {
// Client
$ch = curl_init('http://example.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOSIGNAL, 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";
}
} else {
// Server
sleep(10);
echo "Done.";
}
?>
其他一些技巧:
1. 根据经验总结:cURL 版本 >= libcurl/7.21.0 时,毫秒级超时设置必定生效,请务必注意。
2. curl_multi 并行调用时毫秒级超时可能存在不准的问题,单次访问支持ms级超时,但多路复用并行调多个接口时可能失效。
[流处理方式访问HTTP]
除curl外,开发者常直接使用fsockopen或文件操作函数处理HTTP协议,因此对这些方式的超时处理同样不可或缺。
通常连接超时可直接设置,但流数据的读取超时需要单独进行逻辑处理。
自己写代码处理:
$tmCurrent = gettimeofday();
$intUSGone = ($tmCurrent['sec'] - $tmStart['sec']) * 1000000
+ ($tmCurrent['usec'] - $tmStart['usec']);
if ($intUSGone > $this->_intReadTimeoutUS) {
return false;
}
或者使用内置流处理函数 stream_set_timeout() 和 stream_get_meta_data() 处理:
<?php
// Timeout in seconds
$timeout = 5;
$fp = fsockopen("example.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);
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;
}
}</code></pre>
file_get_contents 超时:
<pre class="brush:php;"><code class="language-php">
<?php
$timeout = array(
'http' => array(
'timeout' => 5
//设置一个超时时间,单位为秒
)
);
$ctx = stream_context_create($timeout);
$text = file_get_contents("http://example.com/", 0, $ctx);
?>
fopen 超时:
<?php
$timeout = array(
'http' => array(
'timeout' => 5
//设置一个超时时间,单位为秒
)
);
$ctx = stream_context_create($timeout);
if ($fp = fopen("http://example.com/", "r", false, $ctx)) {
while( $c = fread($fp, 8192)) {
echo $c;
}
fclose($fp);
}
?>
以上就是小编为大家带来的浅谈php处理后端&接口访问超时的解决方法全部内容了,希望大家多多支持本站~