首页 > 编程 > PHP > 正文

php中curl封装

2019-11-11 00:48:08
字体:
来源:转载
供稿:网友
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;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表