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

相关文章:

  • 2021-06-10
  • 2021-06-14
  • 2021-05-05
  • 2022-02-23
  • 2021-09-30
猜你喜欢
  • 2022-12-23
  • 2021-05-20
  • 2021-08-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案