php之curl實現http請求(支持GET和POST)


php之curl實現http請求(支持GET和POST)

/**
 * curl實現http請求(支持GET和POST)
 * 作者:myth
 * function curl_request ($url[, $post=null, $timeout=10000, $config=array()])
 * @param $url      接口地址
 * @param $post     接口參數和值若為get方式則為空,默認為null
 * @param $timeout  超時時間,默認為10000
 * @param $config =     [
 *   //選填,頭文件信息,默認看文檔
 *   'httpheader'    =>    [
 *   //類型
 *   'Content-type: text/plain',
 *   //長度
 *   'Content-length: 100'
 *   ],
 *   //選填,用於偽裝系統信息和瀏覽器信息
 *   'useragent'     =>    '',
 *   //選填,cookie信息
 *   'cookie'        =>    [],
 *   //是否輸出數據流頭文件信息
 *   'header'        =>    [],
 *   //是否進行編碼
 *   'encoding'      =>    [
 *   //編碼為
 *   'to'   =>   '',
 *   //原編碼或者原編碼的可能數組
 *   'from' =>   ''或者[]
 *   ],
 *   ]                配置信息默認為array()
 * @return array|bool|string
 */
function curl_request ($url, $post=null, $timeout=10000, $config=array()) {

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //返回原生的(Raw)輸出
    curl_setopt($ch, CURLOPT_HEADER, false); //啟用時會將頭文件的信息作為數據流輸出

    //設置https
    if (substr($url, 0, 5)=='https') {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//禁用后curl將終止從服務端進行驗證
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    }

    //設置post內容
    if (!empty($post)) {
        if (!empty($config['encoding'])) {
            $post = mb_convert_encoding($post, $config['encoding']['from'], $config['encoding']['to']);
        }
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    }

    //設置超時時間
    if (!empty($timeout)) {
        curl_setopt($ch, CURLOPT_NOSIGNAL, true);
        curl_setopt($ch, CURLOPT_TIMEOUT_MS, $timeout);
    }

    //設置httpheader
    if (!empty($config['httpheader'])) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $config['httpheader']);
    }

    //設置useragent
    if (!empty($config['useragent'])) {
        curl_setopt($ch, CURLOPT_USERAGENT, $config['useragent']);
    }

    //攜帶cookie訪問
    if (!empty($config['cookie'])) {
        curl_setopt($ch, CURLOPT_COOKIE, $config['cookie']);
    }

    // 設置代理服務器(測試用)
    //curl_setopt($ch,CURLOPT_PROXY,'127.0.0.1:8888');

    //
    if (!empty($config['header']) || (isset($config['cookie']) && !empty($config['cookie']))) {
        curl_setopt($ch, CURLOPT_HEADER, true); //啟用時會將頭文件的信息作為數據流輸出
        $content = curl_exec($ch);
        list($header, $body) = explode("\r\n\r\n", $content, 2); // 解析HTTP數據流
        preg_match("/set\-cookie:([^\r\n]*)/i", $header, $matches); // 解析COOKIE
        $cookie = $matches[1];
        $result = array('header'=>$header, 'cookie'=>$cookie, 'body'=>$body);
    } else {
        $result = curl_exec($ch);
    }

    curl_close($ch);

    //轉換編碼格式
    if (!empty($config['encoding'])) {
        if (is_array($result)) {
            foreach ($result as $k => $v) {
                $result[$k] = mb_convert_encoding($v, $config['encoding']['to'], $config['encoding']['from']);
            }
        } else {
            $result = mb_convert_encoding($result, $config['encoding']['to'], $config['encoding']['from']);
        }
    }

    return $result;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM