PHPcurl的post/get请求

post/get请求

function getCurl($url,$data=null,$method='post',$https=true){
    //1. 初始化
    $ch = curl_init();
    //2.设置参数
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//只获取页面内容,但不输出 return transfer
    if($https){
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
    }
    if($method=='post'){
        curl_setopt($ch,CURLOPT_POST,true);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
    }
    //3.调用接口
    $res = curl_exec($ch);

    if(curl_errno($ch)){
        //请求失败,返回错误信息
        return curl_error($ch);
    }
    //4.关闭curl
    curl_close($ch);
    return $res;
}

猜你喜欢

转载自www.cnblogs.com/wangzhaobo/p/9491749.html