Loading

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;
}
posted @ 2018-08-17 09:58  王召波  阅读(729)  评论(0编辑  收藏  举报