function curl_res_data2($url, $params = '', string $method = 'GET', array $header = array(), int $timeout = 3)
{
$data = array();
$ch = curl_init();
switch (strtoupper($method)) {
case 'GET':
if (is_array($params)) {
$url = $url . '?' . http_build_query($params);
}
break;
case 'POST':
if (is_array($params)) {
$params = http_build_query($params);
}
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
if (!empty($header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
} else {
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded; charset=utf-8',
'Content-Length:' . strlen($params)
));
}
break;
case 'JSON':
if (is_array($params)) {
$params = json_encode($params, 320);
}
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
if (!empty($header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
} else {
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Content-Length:' . strlen($params)
));
}
break;
case 'FILE':
if (is_array($params)) {
foreach ($params as $k => $v) {
$chfile = curl_file_create(realpath($v), mime_content_type($v), basename($v));
$data[is_int($k) ? "file_" . ($k + 1) : $k] = $chfile;
}
} else {
$file_path = realpath($params);
$fileName = basename($file_path);
$mimeType = mime_content_type($file_path);
$data = array(
'file' => curl_file_create($file_path, $mimeType, $fileName)
);
}
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
break;
default:
throw new Exception('不支持的请求方式~');
}
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => $timeout,
CURLOPT_TIMEOUT => $timeout * 5,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36'
));
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
return $response ? json_decode($response, true) : false;
}
function curl_res_data($url, $params = '', string $method = 'GET', array $header = array(), int $timeout = 3)
{
$data = array();
$ch = curl_init();
switch (strtoupper($method)) {
case 'GET':
if (is_array($params)) {
$url = $url . '?' . http_build_query($params);
}
break;
case 'POST':
if (is_array($params)) {
$params = http_build_query($params);
}
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
if (!empty($header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
break;
case 'JSON':
if (is_array($params)) {
$params = json_encode($params, 320);
}
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Content-Length:' . strlen($params)
));
break;
case 'FILE':
if (is_array($params)) {
foreach ($params as $k => $v) {
$chfile = curl_file_create(realpath($v), mime_content_type($v), basename($v));
$data[is_int($k) ? "file_" . ($k + 1) : $k] = $chfile;
}
} else {
$file_path = realpath($params);
$fileName = basename($file_path);
$mimeType = mime_content_type($file_path);
$data = array(
'file' => curl_file_create($file_path, $mimeType, $fileName)
);
}
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
break;
default:
throw new Exception('不支持的请求方式~');
}
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => $timeout,
CURLOPT_TIMEOUT => $timeout * 5,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36'
));
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
return $response ? json_decode($response, true) : false;
}
/**
* @param $url
* @param array $params
* @param string $method
* @param array $header
* @param int $timeOut
* @return bool|mixed
*/
function curl_res_data($url, $params = '', string $method = 'GET', array $header = array(), int $timeOut = 3)
{
$httpInfo = array();
//curl_init()初始化一个新的会话,返回一个CURL句柄供curl_setopt(), curl_exec(),和 curl_close() 函数使用
$ch = curl_init();
switch (strtoupper($method)) {
case 'GET':
if (is_array($params)) {
$url = $url . '?' . http_build_query($params);
}
curl_setopt($ch, CURLOPT_URL, $url);//获取的URL地址
break;
case 'POST':
if (is_array($params)) {
$params = http_build_query($params);
}
curl_setopt($ch, CURLOPT_URL, $url);//获取的URL地址
curl_setopt($ch, CURLOPT_POST, true);//发送POST请求
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);//请求参数
if (!empty($header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
break;
case 'JSON':
if (is_array($params)) {
$params = json_encode($params, 320);
}
curl_setopt($ch, CURLOPT_URL, $url);//获取的URL地址
curl_setopt($ch, CURLOPT_POST, true);//发送POST请求
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);//请求参数
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Content-Length:' . strlen($params)
));
break;
case 'FILE':
if (is_array($params)) {
//支持多文件上传
foreach ($params as $k => $v) {
//$filePath = $v;
//删除多余的./../链接符号,并返回文件绝对路径名
$filePath = realpath($v);
//获取文件名带后缀
$fileName = basename($filePath);
//获取文件名不带后缀
//$fileName = pathinfo($filePath, PATHINFO_FILENAME);
//获取文件mime类型
$mimeType = mime_content_type($filePath);
//创建一个CURLFile, 用与上传文件
$chfile = curl_file_create($filePath, $mimeType, $fileName);
if (is_int($k)) {
$k += 1;
$data['file_' . $k] = $chfile;
} else {
$data[$k] = $chfile;
}
}
} else {
$file_path = realpath($params);//删除多余的./../链接符号,返回文件绝对路径名
$fileName = basename($file_path);//获取带后缀的文件名
$mimeType = mime_content_type($file_path);//获取文件mime类型
//创建一个 CURLFile 对象, 用与上传文件。
$data = array(
'file' => curl_file_create($file_path, $mimeType, $fileName)
);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
break;
default:
return 'Error: 不支持的请求方式~';
exit();
}
//显示header头文件的信息 true显示,false不显示
curl_setopt($ch, CURLOPT_HEADER, false);
//将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeOut);//设置超时限制防止死循环,如果设置为0,则无限等待。
curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut * 10);//设置cURL允许执行的最长秒数
//跳过https证书验证
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
//CURL_HTTP_VERSION_1_0 (强制使用 HTTP/1.0)或CURL_HTTP_VERSION_1_1 (强制使用 HTTP/1.1)
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
//在HTTP请求中伪造一个"User-Agent: "头
//curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36');
$response = curl_exec($ch);// 执行cURL会话
$error = curl_error($ch);//错误信息
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$httpInfo = array_merge($httpInfo, curl_getinfo($ch));
curl_close($ch);//关闭cURL资源,并且释放系统资源
if (!$response) return false;
return json_decode($response, true);//Json对象转换数组
}
/**
* @param $url
* @param null $params
* @param bool $https
* @param int $timeOut
* @return mixed
*/
public static function getUrl($url, $params = null, $timeOut = 3)
{
$httpInfo = array();
if (is_array($params)) {
$url = $url . '?' . http_build_query($params);
}
$ch = curl_init();//初始化一个cURL会话
curl_setopt($ch, CURLOPT_URL, $url);//获取的URL地址
//设置超时限制防止死循环,发起连接前等待的时间,如果设置为0,则无限等待。
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeOut);
//设置cURL允许执行的最长秒数
curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut * 10);
//将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//显示header头文件的信息 true显示,false不显示
curl_setopt($ch, CURLOPT_HEADER, false);
//跳过https证书验证
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//在HTTP请求中伪造一个"User-Agent: "头(浏览器请求建议开启)
//curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
//CURL_HTTP_VERSION_1_0 (强制使用 HTTP/1.0)或CURL_HTTP_VERSION_1_1 (强制使用 HTTP/1.1)
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
$output = curl_exec($ch);//执行会话
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);//http状态码
$httpInfo = array_merge($httpInfo, curl_getinfo($ch));//获取cURL连接资源句柄的信息。
curl_close($ch);//关闭cURL资源,并且释放系统资源
if (!$output) return false;
return json_decode($output, true);//Json对象转换数组
}
/**
* @param $url
* @param $params
* @param bool $https
* @param int $timeOut
* @param array $header
* @return mixed
*/
public static function postUrl($url, $params, $timeOut = 3, array $header = array())
{
$httpInfo = array();
if (is_array($params)) {
$params = http_build_query($params);
}
$ch = curl_init();//初始化一个cURL会话
curl_setopt($ch, CURLOPT_URL, $url);//获取的URL地址
curl_setopt($ch, CURLOPT_POST, true);//发送POST请求
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
if ($header) {
//设置Http请求头需以数组方式进行设置:
//array('Content-Type: application/json; charset=utf-8', 'Content-Length:' . strlen($params));
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
//设置超时限制防止死循环,发起连接前等待的时间,如果设置为0,则无限等待
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeOut);
//设置cURL允许执行的最长秒数
curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut * 10);
//将curl_exec()获取的信息以文件流的形式返回,而不是直接输出
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//显示header头文件的信息 true显示,false不显示
curl_setopt($ch, CURLOPT_HEADER, false);
//跳过https证书验证
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//CURL_HTTP_VERSION_1_0 (强制使用 HTTP/1.0)或CURL_HTTP_VERSION_1_1 (强制使用 HTTP/1.1)
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
//在HTTP请求中伪造一个"User-Agent: "头 (如果是浏览器发请求建议开启)
//curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$output = curl_exec($ch);//执行会话
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);//http状态码
$httpInfo = array_merge($httpInfo, curl_getinfo($ch));//获取cURL连接资源句柄的信息。
curl_close($ch);//关闭cURL资源,并且释放系统资源
if (!$output) return false;
return json_decode($output, true);//Json对象转换数组
}
/**
* @param $url
* @param $params
* @param bool $https
* @param int $timeOut
* @return mixed
*/
public static function jsonPostUrl($url, $params, $timeOut = 3)
{
$httpInfo = array();
if (is_array($params)) {
//数组转json解决中文乱码 + 不转义反斜杠
//JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES = 320
$params = json_encode($params, 320);
}
$ch = curl_init();//初始化一个cURL会话
curl_setopt($ch, CURLOPT_URL, $url);//获取的URL地址
curl_setopt($ch, CURLOPT_POST, true);//发送POST请求
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
//设置Http请求头需以数组方式进行设置:
//array('Content-Type: application/json; charset=utf-8','Content-Length:' . strlen($params));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8', 'Content-Length:' . strlen($params)
));
//设置超时限制防止死循环,发起连接前等待的时间,如果设置为0,则无限等待
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeOut);
//设置cURL允许执行的最长秒数
curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut * 10);
//将curl_exec()获取的信息以文件流的形式返回,而不是直接输出
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//显示header头文件的信息 true显示,false不显示
curl_setopt($ch, CURLOPT_HEADER, false);
//跳过https证书验证
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//在HTTP请求中伪造一个"User-Agent: "头
//curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
//CURL_HTTP_VERSION_1_0 (强制使用 HTTP/1.0)或CURL_HTTP_VERSION_1_1 (强制使用 HTTP/1.1)
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
$output = curl_exec($ch);//执行会话
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);//http状态码
$httpInfo = array_merge($httpInfo, curl_getinfo($ch));//获取cURL连接资源句柄的信息。
curl_close($ch);//关闭cURL资源,并且释放系统资源
if (!$output) return false;
return json_decode($output, true);//Json对象转换数组
}
/**
* curl文件上传(支持多文件上传)
* @param $uploadUrl
* @param array $file
* @return bool|mixed
*/
function curl_files($uploadUrl, array $file)
{
$data = array();
foreach ($file as $k => $v) {
$filePath = realpath($v);
$fileName = basename($filePath);
$mimeType = mime_content_type($filePath);
$chfile = curl_file_create($filePath, $mimeType, $fileName);
if (is_int($k)) {
$data['file_' . $k] = $chfile;
} else {
$data[$k] = $chfile;
}
}
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $uploadUrl,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1
));
$output = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $output ? json_decode($output, true) : false;
}
/**
* curl文件上传(支持多文件上传)
* @param $uploadUrl 上传url
* 上传文件 索引数组:$file = ['../data.csv', '../data22.csv'];
* @param array $file
* @return bool|string
*/
function curl_file($uploadUrl, array $file)
{
$httpInfo = array();
$ch = curl_init();
// 设置上传URL
curl_setopt($ch, CURLOPT_URL, $uploadUrl);
foreach ($file as $k => $v) {
//$filePath = $v;
//删除多余的./../链接符号,并返回文件绝对路径名
$filePath = realpath($v);
//获取文件名带后缀
$fileName = basename($filePath);
//获取文件名不带后缀
//$fileName = pathinfo($filePath, PATHINFO_FILENAME);
//获取文件mime类型
$mimeType = mime_content_type($filePath);
//创建一个CURLFile, 用与上传文件
$chfile = curl_file_create($filePath, $mimeType, $fileName);
if (is_int($k)) {
$data['file_' . $k] = $chfile;
} else {
$data[$k] = $chfile;
}
}
// 发送POST请求
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
//将获取的信息以文件流的形式返回,而不是直接输出在浏览器
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//跳过https证书验证
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//CURL_HTTP_VERSION_1_0 (强制使用 HTTP/1.0)或CURL_HTTP_VERSION_1_1 (强制使用 HTTP/1.1)
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
// 执行curl请求
$output = curl_exec($ch);
//http状态码
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//获取cURL连接资源句柄的信息。
$httpInfo = array_merge($httpInfo, curl_getinfo($ch));
// 检查是否出现错误
if (curl_errno($ch)) {
return 'Error: ' . curl_error($ch);
}
// 关闭curl句柄
curl_close($ch);
if (!$output) return false;
return json_decode($output, true);//Json对象转换数组
}
/**
* Curl Ftp上传
* @return int
*
*/
public static function ftpUploads()
{
$file = $_FILES['file'];
$username = '***.***.***.***'; //用户名
$password = '***************'; //密码
$url = trim('ftp://***.**.***.**:21/test_01.txt'); //需要上传到服务器的地址/文件名(文件名必填)
//$filePath = realpath('D:\桌面\test_01.txt'); //待上传的文件地址
$filePath = realpath($file['tmp_name']); //待上传的文件地址
$fp = fopen($filePath, 'r');//打开文件或者 URL(只读方式打开)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);//不显示header头信息
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);//用户名和密码
curl_setopt($ch, CURLOPT_UPLOAD, true);//允许文件上传
curl_setopt($ch, CURLOPT_INFILE, $fp);//上传文件的时候需要读取的文件地址,值是一个资源类型
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filePath));//设定上传文件的大小限制,字节(byte)为单位。
//设置超时限制防止死循环,发起连接前等待的时间,如果设置为0,则无限等待
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
//设置cURL允许执行的最长秒数
curl_setopt($ch, CURLOPT_TIMEOUT, 10 * 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//dump(curl_error($ch));//输出报错信息,需在curl_exec()前使用,不然错误信息无法输出
curl_exec($ch);
if (!curl_errno($ch)) {
$res = 1;
} else {
//'Curl error: ' . curl_error($ch);
$res = 0;
}
curl_close($ch);
fclose($fp);
return $res;
}
/**
*Curl Ftp下载(没测试,不知道是否可用)
*/
public static function ftpDownlods()
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "ftp://192.***.*.***:21/文件名");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "用户名:密码");//FTP用户名:密码
// Sets up the output file
$outfile = fopen('test.txt', 'wb');//保存到本地的文件名
curl_setopt($ch, CURLOPT_FILE, $outfile);
//设置超时限制防止死循环,发起连接前等待的时间,如果设置为0,则无限等待
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
//设置cURL允许执行的最长秒数
curl_setopt($ch, CURLOPT_TIMEOUT, 10 * 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$rtn = curl_exec($ch);
if (!curl_errno($ch)) {
$res = 1;
} else {
//'Curl error: ' . curl_error($ch);
$res = 0;
}
curl_close($ch);
fclose($outfile);
return $res;
}
网友评论