2020-04-27 CURL类

1、Get请求

public static function get($url){

    $oCurl = curl_init();

    if(stripos($url,"https://") !== FALSE){

        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);

        curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);

        curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1

    }

    curl_setopt($oCurl, CURLOPT_URL, $url);

    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );

    curl_setopt($oCurl, CURLOPT_TIMEOUT, 30);

    $user_agent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3641.400 QQBrowser/10.4.3284.400";

    curl_setopt($oCurl, CURLOPT_USERAGENT, $user_agent);

    curl_setopt($oCurl, CURLOPT_ENCODING, "gzip");

    $sContent = curl_exec($oCurl);

    $aStatus = curl_getinfo($oCurl);

    $error = curl_error($oCurl);

    curl_close($oCurl);

    if(!empty($error)){

        throw new Exception($error);

    }

    if(intval($aStatus["http_code"])==200){

        return $sContent;

    }else{

        return false;

    }

}


2、Post请求



public static function post($url, array $param, $post_file=false)

{

    $oCurl = curl_init();

    if(stripos($url,"https://")!==FALSE){

        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);

        curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);

        curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1

    }

    if (is_string($param) || $post_file) {

        $strPOST = $param;

    } else{

        $aPOST = array();

        foreach($param as $key=>$val){

            $aPOST[] = $key."=".urlencode($val);

        }

        $strPOST =  join("&", $aPOST);

    }

    curl_setopt($oCurl, CURLOPT_URL, $url);

    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );

    curl_setopt($oCurl, CURLOPT_POST,true);

    curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);

    curl_setopt($oCurl, CURLOPT_TIMEOUT, 30);

    $sContent = curl_exec($oCurl);

    $aStatus = curl_getinfo($oCurl);

    curl_close($oCurl);

    dump(json_decode($sContent));

    if(intval($aStatus["http_code"])==200){

        return $sContent;

    }else{

        return false;

    }

}


3、XML请求


public static function getXml($url)

{

    $oCurl = curl_init();

    if(stripos($url,"https://")!==FALSE){

        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);

        curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);

        curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1

    }

    curl_setopt($oCurl, CURLOPT_URL, $url);

    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );

    curl_setopt($oCurl, CURLOPT_TIMEOUT, 30);

    $user_agent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3641.400 QQBrowser/10.4.3284.400";

    curl_setopt($oCurl, CURLOPT_USERAGENT, $user_agent);

    curl_setopt($oCurl, CURLOPT_ENCODING, "gzip");

    $sContent = curl_exec($oCurl);

    $aStatus = curl_getinfo($oCurl);

    curl_close($oCurl);

    $xml = @simplexml_load_string($sContent);

    if($xml){

        return json_decode(json_encode($xml), true);

    }else{

        return false;

    }

}

你可能感兴趣的:(2020-04-27 CURL类)