<form id="55n5d"></form>
    <form id="55n5d"></form> <noframes id="55n5d">

    <em id="55n5d"><address id="55n5d"></address></em>

        <form id="55n5d"></form>

        curl的get和post的使用方法

        php by 黃業興 at 2020-01-08

        get:

        public function get_curl(){
            $url = "";
            $parameter = array();
            $ch = curl_init();
            //設置選項,包括URL
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $parameter);
        
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//繞過ssl驗證
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_TIMEOUT,6);
            //執行并獲取HTML文檔內容
            $result = curl_exec($ch);
            if (curl_errno($ch)) {
                print curl_error($ch);
            }
            //釋放curl句柄
            curl_close($ch);
            return $result;
        }
        

        post:

        public function post_curl(){
            $url = "";
            $parameter =  array();
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $parameter);
        
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //繞過ssl驗證
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        
            $result = curl_exec($ch);
            if (curl_errno($ch)) {
                print curl_error($ch);
            }
            //釋放curl句柄
            curl_close($ch);
            return $result;
        }
        

        模擬瀏覽器訪問

            public function curl_request($url,$data=array(),$build=false){
        
                $ch = curl_init($url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                if(!empty($data)){
                    $build && $data = http_build_query($data);
                    // RCA:未注意Curl-library Post 1024以上字節時的HTTP/1.1特性導致 HessianPHP 傳輸數據失敗
                    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
                    curl_setopt($ch, CURLOPT_POST, 1);
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                }else{
                    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
                    //curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
                }
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//繞過ssl驗證
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
                curl_setopt($ch, CURLOPT_TIMEOUT, 25);
                $response = curl_exec($ch);
                if(curl_errno($ch)){
                    throw new \Exception($url.':'.curl_error($ch));
                    die(curl_error($ch));
                }
                curl_close($ch);
                $encode = mb_detect_encoding($response, array('UTF-8','GB2312','GBK'));
                if($encode != 'UTF-8'){
                    $response = $this->to_utf($response);
                }
                return $response;
        
            }
        

        這里還有個轉換字符的函數

        public function to_utf($str,$ignore=true){
                if($ignore){
                    return iconv("GBK","UTF-8//IGNORE",$str);
                }else{
                    return iconv("GBK","UTF-8",$str);
                }
            }
        

        請關注我們微信公眾號:mw748219


        永久免费看A片无码网站宅男

          <form id="55n5d"></form>
          <form id="55n5d"></form> <noframes id="55n5d">

          <em id="55n5d"><address id="55n5d"></address></em>

              <form id="55n5d"></form>