php curl 超时处理

xiaoxiao2021-02-28  135

php curl处理请求超时

背景:写了一个api管理的工具,增加了api请求的的工具,某些接口请求时间比较长,某些接口时间必须要短,希望超过超时时间的请求返回其它的状态码,要跟失败或者curl失败区分开,本质上虽然都是curl失败但是从需求角度要区分开,但是php的curl没有类似事件的一些操作

通过curl的curl_errno来判断

$curl = curl_init($url); curl_setopt ( $curl, CURLOPT_POST, 1 ); curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt ( $curl, CURLOPT_POSTFIELDS, $class->getPostParam() ); curl_setopt ( $curl, CURLOPT_TIMEOUT, $timeout); curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt ( $curl, CURLOPT_HEADER, true); $response = curl_exec($curl); if($response === false) { if(curl_errno($curl) == CURLE_OPERATION_TIMEDOUT) { //超时的处理代码 } }

转载请注明原文地址: https://www.6miu.com/read-27311.html

最新回复(0)