PHP实现curl post和get详解编程语言

CURL这里就不说明了。以下是简单案例

一、POST

//初始化 
    $curl = curl_init(); 
    //设置抓取的url 
    curl_setopt($curl, CURLOPT_URL, 'http://www.baidu.com'); 
    //设置头文件的信息作为数据流输出 
    curl_setopt($curl, CURLOPT_HEADER, 1); 
    //设置获取的信息以文件流的形式返回,而不是直接输出。 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    //设置post方式提交 
    curl_setopt($curl, CURLOPT_POST, 1); 
    //设置post数据 
    $post_data = array( 
        "username" => "coder", 
        "password" => "12345" 
        ); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); 
    //执行命令 
    $data = curl_exec($curl); 
    //关闭URL请求 
    curl_close($curl); 
    //显示获得的数据 
    print_r($data);
POST 
 private function _set_headers($user_agent , $client_ip ) 
    { 
        $this->headers = [ 
            'Content-Type: application/json;charset=UTF-8', 
            'Authorization: '. self::APP_KEY.':'.$this->sign, 
            'Date: ' .$this->gmt_date, 
            'Content-Length: ' .$this->content_len, 
            'X-FORWARDED-FOR:'. $client_ip, 
            'CLIENT-IP:'. $client_ip, 
            'User-Agent:'.$user_agent 
        ]; 
    } 
 
    private function http_post_json($url, $jsonStr) 
    { 
        $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_POST, 1); 
        curl_setopt($ch, CURLOPT_URL, $url); 
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers); 
        //curl_setopt($ch,CURLOPT_PROXY,'118.0.0.1:8888'); 
        $response = curl_exec($ch); 
 
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
        curl_close($ch); 
        return ['code' => $httpCode, 'msg' => $response]; 
    }

二、GET

//初始化 
    $curl = curl_init(); 
    //设置抓取的url 
    curl_setopt($curl, CURLOPT_URL, 'http://www.baidu.com'); 
    //设置头文件的信息作为数据流输出 
    curl_setopt($curl, CURLOPT_HEADER, 1); 
    //设置获取的信息以文件流的形式返回,而不是直接输出。 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    //执行命令 
    $data = curl_exec($curl); 
    //关闭URL请求 
    curl_close($curl); 
    //显示获得的数据 
    print_r($data);
GET 
 
 //get获取合力token 
    private function http_get_token() 
    { 
        $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_URL, self::GET_ACCESS_TOKEN_URL); 
        curl_setopt($ch, CURLOPT_HEADER, 1); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        $data = curl_exec($ch); 
        var_dump($data);die; 
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
        curl_close($ch); 
        return ['code' => $httpCode, 'msg' => CJSON::decode($data, true)]; 
    }
/** 
     * 发起HTTP GET请求 
     *  
     * @param string $url 请求URL 
     * @param string $host 请求主机HOST 
     * @param array $params 请求参数 
     */ 
    public function http_get($url, $host = '', $params = array(), $timeout=3, $ms = false){ 
        if(is_array($params)){ 
            $getString= ''; 
            $getParams = array(); 
            foreach ($params as $key=>$val) { 
                $getParams[] = $key.'='.($val); 
                $getString = implode('&', $getParams); 
            } 
            //$getString = http_build_query($params); 
        }else{ 
            $getString = $params;    
        } 
        $url .= "?" . $getString; 
        if (function_exists('curl_init')) { 
            $ch = curl_init(); 
            curl_setopt($ch, CURLOPT_URL, $url); 
            if(!empty($host)){ 
                curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: ' . $host)); 
            } 
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
            curl_setopt($ch, CURLOPT_USERAGENT, 'PHP5 Client ver: ' . phpversion()); 
            if(true === $ms){ 
                curl_setopt($ch, CURLOPT_TIMEOUT_MS, $timeout); 
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $timeout); 
            }else{ 
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
                curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); 
            } 
            $result = curl_exec($ch); 
            curl_close($ch); 
        } else { 
            // 发起请求 
            $handle = fopen ( $url, 'r' ); 
            $result = ""; 
            if ($handle) { 
                while ( ! feof ( $handle ) ) { 
                    $result .= fread ( $handle, 4096 ); 
                } 
                fclose ( $handle ); 
            } 
        } 
        return $result; 
    }
/** 
     * post请求 
     * @param $url string  
     * @param $host string 
     */ 
    private function http_post($url, $host = '', $params = array(), $timeout=5, $ms = false){ 
        if(is_array($params)){ 
            $postString = http_build_query($params); 
        }else{ 
            $postString = $params;   
        } 
 
        if (function_exists('curl_init')) { 
            $ch = curl_init(); 
            curl_setopt($ch, CURLOPT_URL, $url); 
            if(!empty($host)){ 
                curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: ' . $host)); 
            } 
            curl_setopt($ch, CURLOPT_POST, 1); 
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postString); 
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
            curl_setopt($ch, CURLOPT_USERAGENT, 'YOUKU.COM PREMIUM API PHP5 Client ver: ' . phpversion()); 
            if(true === $ms){ 
                curl_setopt($ch, CURLOPT_TIMEOUT_MS, $timeout); 
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $timeout); 
            }else{ 
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
                curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); 
            } 
            $result = curl_exec($ch); 
            curl_close($ch); 
        } else { 
            $context = array( 
                'http' => array( 
                    'method' => 'POST', 
                    'header' => 'Content-type: application/x-www-form-urlencoded'    . "/r/n". 
                    'Host: '.$host . "/r/n". 
                    'User-Agent: PHP5 Client ver: ' . phpversion() . "/r/n". 
                    'Content-length: ' . strlen($postString), 
                    'content' => $postString 
                ) 
            ); 
            $contextId = stream_context_create($context); 
            $handle = fopen($url, 'r', false, $contextId); 
            $result = ''; 
            if ($handle) { 
                while (!feof($handle)) { 
                    $result .= fgets($handle, 4096); 
                } 
                fclose($handle); 
            } 
        } 
 
        return $result;  
    }
   //拨号请求 
    function doCurlGetRequest($url, $data = [], $header = [], $timeout = 5){ 
        if($url == "" || $timeout <= 0){ 
            return false; 
        } 
        $url = $url.'?'.http_build_query($data); 
        $curl = curl_init((string)$url); 
        curl_setopt($curl, CURLOPT_HEADER, false); 
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书 
        curl_setopt($curl, CURLOPT_RETURNTRANSFER,true); 
        curl_setopt($curl, CURLOPT_TIMEOUT, (int)$timeout); 
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header); //添加自定义的http header 
        return curl_exec($curl); 
    }

 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/17176.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论