curl的get和post的使用方法

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);
        }
    }

put使用

$ch = curl_init();
$header[] = "Content-type: application/json";
curl_setopt($ch, CURLOPT_URL, $url); //定义请求地址
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//定义是否直接输出返回流
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); //定义请求类型,必须为大写
//curl_setopt($ch, CURLOPT_HEADER,1); //定义是否显示状态头 1:显示 ; 0:不显示
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//定义header
curl_setopt($ch, CURLOPT_USERPWD, "$this->api_key:$this->password");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //定义提交的数据
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$output = curl_exec($ch);
curl_close($ch);//关闭
标签:

发表评论