php中curl封装

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lihaitao_1/article/details/54922629
public static function postcurl($data){
    $ch = curl_init();
    // 设置curl允许执行的最长秒数
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    // 获取的信息以文件流的形式返回,而不是直接输出。
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    // 从证书中检查SSL加密算法是否存在
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
    if($data['type'] == "post") {
        //发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。
        $fields_string = '';
        foreach($data['fileds'] as $key => $value){
            $fields_string.=$key.'='.$value.'&';
        }
        $fields_string = rtrim($fields_string , '&');
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $data['url']);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
    }else{
        curl_setopt($ch, CURLOPT_URL, $data['url']);
    }
    $res = curl_exec($ch);
    return  $res;
}

猜你喜欢

转载自blog.csdn.net/lihaitao_1/article/details/54922629