zoukankan      html  css  js  c++  java
  • 一个带重试次数的curl 函数

    <?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;
    }
    
  • 相关阅读:
    什么是缓存?
    什么是反射?
    Nginx配置文件详解
    数据库插入,修改出现中文乱码解决办法
    hadoop的HA机制+zookeeper
    cascading--wordcount
    心情3
    完成用户登录注册功能
    感慨1
    Swing程序设计-初级
  • 原文地址:https://www.cnblogs.com/keygle/p/3217248.html
Copyright © 2011-2022 走看看