zoukankan      html  css  js  c++  java
  • 封装的curl

    function httpRequest($url, $method="GET", $postfields = null, $headers = array(), $debug = false, $timeout=60)
    {
        $method = strtoupper($method);
        $ci = curl_init();
        curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($ci, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
        curl_setopt($ci, CURLOPT_CONNECTTIMEOUT,$timeout); 
        curl_setopt($ci, CURLOPT_TIMEOUT, 7); 
        curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
        switch ($method) {
            case "POST":
                curl_setopt($ci, CURLOPT_POST, true);
                if (!empty($postfields)) {
                    $tmpdatastr = is_array($postfields) ? http_build_query($postfields) : $postfields;
                    curl_setopt($ci, CURLOPT_POSTFIELDS, $tmpdatastr);
                }
                break;
            default:
                curl_setopt($ci, CURLOPT_CUSTOMREQUEST, $method);
                break;
        }
        $ssl = preg_match('/^https:///i',$url) ? TRUE : FALSE;
        curl_setopt($ci, CURLOPT_URL, $url);
        if($ssl){
            curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); 
            curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, FALSE); 
        }
        if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) {
            curl_setopt($ci, CURLOPT_FOLLOWLOCATION, 1);
        }
        curl_setopt($ci, CURLOPT_MAXREDIRS, 2);
        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ci, CURLINFO_HEADER_OUT, true);
        $response = curl_exec($ci);
        $requestinfo = curl_getinfo($ci);
        $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
        curl_close($ci);
        return $response;
    }
  • 相关阅读:
    arcEngine classic code(2)
    VisualGraph文档
    基于.net2 的CAD 绘图控件virtualGraph(2)
    沈阳三维GIS软件开发人员
    arcEngine + .net 2 AccessViolationException
    数据库事务并发带来的问题
    理解 WPF Dispatcher
    扇入与扇出
    IComparable 与 IComparer
    同步、异步、多线程
  • 原文地址:https://www.cnblogs.com/shineguang/p/13099147.html
Copyright © 2011-2022 走看看