curl get & post 示例

CURL 个人觉得没必要背,所以我记录在此两个方法

GET请求:

function curl_get($url,$is_ssl=FALSE) {//模拟浏览器get请求
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5000);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, Is_SSL);//ssl 不验证
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, Is_SSL);//ssl 不验证

    curl_setopt($ch, CURLOPT_URL,$url );
    $res = curl_exec($ch);
    echo $res;
}

POST请求:

function curl_post($url, $params=[], $header=[]) {//模拟浏览器post请求
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5000);
    curl_setopt($ch, CURLOPT_POST, 1 );

    if(is_array($params) && !empty($params) ) {
        $post_param = http_build_query($params);//构造url参数地址
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_param);
    }

    if(is_array($header) && !empty($header)) {
      curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//请求的头信息数组
    }

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, Is_SSL);//ssl 不验证
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, Is_SSL);//ssl 不验证
    curl_setopt($ch, CURLOPT_URL,$url );
    $res = curl_exec($ch);

    echo $res;
}

 

posted @ 2016-08-09 21:59  Zell~Dincht  阅读(480)  评论(0编辑  收藏  举报