PHP通用CURL类,可POST/GET/文件传输
function do_curl($url, $params = array(), $upload = false, $type = ‘POST‘) { $method = strtoupper($type); if ($method == ‘GET‘) { $url = "{$url}?" . http_build_query($params); } $ch = curl_init(); // $header[] = "User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36"; // //定义请求类型 // curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); // 是否检测服务器的证书是否由正规浏览器认证过的授权CA颁发的 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 是否检测服务器的域名与证书上的是否一致 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // var_dump($params);die; if ($method == ‘POST‘) { if ($upload) { //设置上传文件 $file = new \CURLFile($upload[‘tmp_name‘], $upload[‘type‘], $upload[‘name‘]); $params[$upload[‘get_name‘]] = $file; } @curl_setopt($ch, CURLOPT_POST, 1); @curl_setopt($ch, CURLOPT_POSTFIELDS, $params); } $result = curl_exec($ch); curl_close($ch); if ($result === null || $result === false) { $this->error(‘操作出错!‘); } else { return $result; } }
原文地址:https://www.cnblogs.com/bk233/p/9508024.html
时间: 2024-10-07 00:32:17