您好,欢迎访问一九零五行业门户网

php请求接口超时如何解决

【http访问】
一般我们访问http方式很多,主要是:curl, socket, file_get_contents() 等方法。
如果碰到对方服务器一直没有响应的时候,我们就悲剧了,很容易把整个服务器搞死,所以在访问http的时候也需要考虑超时的问题。
[ curl 访问http]
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秒。
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如果需要进行毫秒超时,需要增加:
curl_easy_setopt(curl, curlopt_nosignal, 1l);
或者是:
curl_setopt ( $ch,  curlopt_nosignal, true); 是可以支持毫秒级别超时设置的
curl一个毫秒级超时的例子:
<?phpif (!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级超时的,curl_multi并行调多个会不准
[流处理方式访问http]
除了curl,我们还经常自己使用fsockopen、或者是file操作函数来进行http协议的处理,所以,我们对这块的超时处理也是必须的。
推荐教程:php视频教程
以上就是php请求接口超时如何解决的详细内容。
其它类似信息

推荐信息