最新消息:

php curl 重试

curl admin 4475浏览 0评论
<?php
/**
 * [curl 带重试次数]
 * @param  [type]  $url     [访问的url]
 * @param  [type]  $post    [$POST参数]
 * @param  integer $retries [curl重试次数]
 * @return [type]           [description]
 */
function curl($url, $post = null, $retries = 3){
        $curl = curl_init($url);
 
        if(is_resource($curl) === true){
                curl_setopt($curl, CURLOPT_FAILONERROR, true);
                curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
 
                if(isset($post) === true){
                        curl_setopt($curl, CURLOPT_POST, true);
                        curl_setopt($curl, CURLOPT_POSTFIELDS, (is_array($post) === true) ? http_build_query($post, "", "&"): $post);
                }
 
                $result = false;
 
                while(($result === false) && (--$retries > 0)){
                        $result = curl_exec($curl);
                }
 
                curl_close($curl);
        }
 
        return $result;
}

 

转载请注明:爱开源 » php curl 重试

您必须 登录 才能发表评论!