为了开发方便PHP封装简单易用请求接口类curl封装类支持多文件上传,支持json数据传输,直接实例化类调用方法传参即可,省掉了频繁写curl相关参数配置的麻烦…
通常php中curl请求接口方式一般为:/**
* Notes:CURL GET请求
*/
function httpGet($requestUrl){
$urlRs = curl_init();
curl_setopt($urlRs, CURLOPT_URL, $requestUrl);
curl_setopt($urlRs, CURLOPT_FAILONERROR, false);
curl_setopt($urlRs, CURLOPT_RETURNTRANSFER, true);
curl_setopt($urlRs, CURLOPT_TIMEOUT, 10); // 读取URL的超时时间 10s
curl_setopt($urlRs, CURLOPT_CONNECTTIMEOUT, 10); //连接超时时间 10s
//判断是否是HTTPS请求
if(strlen($requestUrl) > 5 && strtolower(substr($requestUrl, 0, 5)) == "https" ){
curl_setopt($urlRs, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($urlRs, CURLOPT_SSL_VERIFYHOST, 0);
}
//发送消息,获取响应
$response = curl_exec($urlRs);
//关闭连接
curl_close($urlRs);
return $response;
}
/**
* Notes:CURL POST请求
*/
function httpPost($requestUrl, $postData){
$urlRs = curl_init();
curl_setopt($urlRs, CURLOPT_URL, $requestUrl);
curl_setopt($urlRs, CURLOPT_FAILONERROR, false);
curl_setopt($urlRs, CURLOPT_RETURNTRANSFER, true);
curl_setopt($urlRs, CURLOPT_POST, true);
curl_setopt($urlRs, CURLOPT_POSTFIELDS, $postData);
curl_setopt($urlRs, CURLOPT_TIMEOUT, 10); // 读取URL的超时时间 10s
curl_setopt($urlRs, CURLOPT_CONNECTTIMEOUT, 10); //连接超时时间 10s
//判断是否是HTTPS请求
if(strlen($requestUrl) > 5 && strtolower(substr($requestUrl, 0, 5)) == "https" ){
curl_setopt($urlRs, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($urlRs, CURLOPT_SSL_VERIFYHOST, 0);
}
//发送消息,获取响应
$reponse = curl_exec($urlRs);
//关闭连接
curl_close($urlRs);
return $reponse;
}
/**
* Notes:json格式 POST请求
*/
function jsonPost($requestUrl, $postData){
$urlRs = curl_init();
$headers = array(
"Content-type:application/json;charset='utf-8'",
"Accept: application/json",
"Cache-Control: no-cache",
"Pragma: no-cache"); // 注意请求头配置
curl_setopt($urlRs, CURLOPT_HTTPHEADER, $headers);
curl_setopt($urlRs, CURLOPT_URL, $requestUrl);
curl_setopt($urlRs, CURLOPT_FAILONERROR, false);
curl_setopt($urlRs, CURLOPT_RETURNTRANSFER, true);
curl_setopt($urlRs, CURLOPT_POST, true);
curl_setopt($urlRs, CURLOPT_POSTFIELDS, $postData);
curl_setopt($urlRs, CURLOPT_TIMEOUT, 10); // 读取URL的超时时间 10s
curl_setopt($urlRs, CURLOPT_CONNECTTIMEOUT, 10); //连接超时时间 10s
//判断是否是HTTPS请求
if(strlen($requestUrl) > 5 && strtolower(substr($requestUrl, 0, 5)) == "https" ){
curl_setopt($urlRs, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($urlRs, CURLOPT_SSL_VERIFYHOST, 0);
}
//发送消息,获取响应
$reponse = curl_exec($urlRs);
//关闭连接
curl_close($urlRs);
return $reponse;
}
为了开发方便PHP封装简单易用请求接口类curl封装类支持多文件上传,支持json数据传输,直接实例化类调用方法传参即可,省掉了频繁写curl相关参数配置的麻烦,具体看下边示例。
demo.php文件:include 'log.php';
include 'PHPcurl.php';
// 调用示例:
//* 一、GET请求:
$url = 'http://test.com/plug/phpcurl/test.php?id=1&name=越加网';
$request = new PHPcurl();
$result = $request->httpGet($url);
print_r($result);//exit;
//*/
// 二、POST请求:
$url = 'http://test.com/plug/phpcurl/test.php';
$params['id'] = 1;
$params['name'] = '越加网';
$files['key'] = 'headimg';
$files['data'] = array('type'=>'image/jpeg','path'=>'E:/www/test/filetest/getfile/1.jpg');
/* 如果是多文件时$files['data']为多维数组,如:
$files['data'][0] = array('type'=>'image/jpeg','path'=>'E:/www/test/filetest/getfile/1.jpg'); // 文件一
$files['data'][1] = array('type'=>'image/jpeg','path'=>'E:/www/test/filetest/getfile/2.jpg'); // 文件二
*/
$request = new PHPcurl();
$result = $request->httpPost($url,$params,false,$files);
print_r($result);
test.php文件(这里上传的文件不做处理只打印接收记录,文件上传接收源码请查看上几篇文章或者在站内搜索“文件上传”关键字查看相关文章):include 'log.php';
loger($_REQUEST);
loger($_FILES);
echo json_encode(array('code'=>10000,'msg'=>'成功'));
PHPcurl.php类文件:/**
* Notes:PHP Curl 请求工具类
* User: ZHL
* Date: 2020/5/26
*/
class PHPcurl{
//读取URL的超时时间 10s
private $timeout = 10;
//连接超时时间 10s
private $connectTimeout = 10;
/**
* Notes:GET请求
*/
public function httpGet($requestUrl){
$urlRs = curl_init();
curl_setopt($urlRs, CURLOPT_URL, $requestUrl);
curl_setopt($urlRs, CURLOPT_FAILONERROR, false);
curl_setopt($urlRs, CURLOPT_RETURNTRANSFER, true);
if ($this->timeout) {
curl_setopt($urlRs, CURLOPT_TIMEOUT, $this->timeout);
}
if ($this->connectTimeout) {
curl_setopt($urlRs, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
}
//判断是否是HTTPS请求
if(strlen($requestUrl) > 5 && strtolower(substr($requestUrl, 0, 5)) == "https" ){
curl_setopt($urlRs, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($urlRs, CURLOPT_SSL_VERIFYHOST, 0);
}
//发送消息,获取响应
$response = curl_exec($urlRs);
//关闭连接
curl_close($urlRs);
return $response;
}
/**
* Notes:POST请求
*/
public function httpPost($requestUrl, $postData, $jsonType=false, $files=array()){
// 有文件
if(isset($files['key']) && isset($files['data']) && $files['key'] && !empty($files['data'])){
if(isset($postData[$files['key']])){
return json_encode(array('code'=>40000,'msg'=>'文件key在其他参数中已被占用'));
}
$key = $files['key'];
$data = $files['data'];
// 单文件
if(isset($data['type']) && isset($data['path']) && $data['type'] && $data['path']){
if(version_compare(phpversion(),'5.5.0') >= 0 && class_exists('CURLFile')){
$postData[$key] = new CURLFile($data['path'],$data['type']);
}else{
$postData[$key] = '@'.$data['path'];//加@符号curl就会把它当成是文件上传处理
}
}else{ // 多文件
foreach($data as $k=>$v){
if(isset($v['path']) && isset($v['type']) && $v['path'] && $v['type']){
if(version_compare(phpversion(),'5.5.0') >= 0 && class_exists('CURLFile')){
$postData[$key.$k] = new CURLFile($v['path'],$v['type']);
}else{
$postData[$key.$k] = '@'.$v['path'];
}
}
}
}
}
//print_r($postData);exit;
$urlRs = curl_init();
// 如果是json格式POST请求
if($jsonType){
$headers = array(
"Content-type:application/json;charset='utf-8'",
"Accept: application/json",
"Cache-Control: no-cache",
"Pragma: no-cache");
curl_setopt($urlRs, CURLOPT_HTTPHEADER, $headers);
}
curl_setopt($urlRs, CURLOPT_URL, $requestUrl);
curl_setopt($urlRs, CURLOPT_FAILONERROR, false);
curl_setopt($urlRs, CURLOPT_RETURNTRANSFER, true);
curl_setopt($urlRs, CURLOPT_POST, true);
curl_setopt($urlRs, CURLOPT_POSTFIELDS, $postData);
if ($this->timeout) {
curl_setopt($urlRs, CURLOPT_TIMEOUT, $this->timeout);
}
if ($this->connectTimeout) {
curl_setopt($urlRs, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
}
//判断是否是HTTPS请求
if(strlen($requestUrl) > 5 && strtolower(substr($requestUrl, 0, 5)) == "https" ){
curl_setopt($urlRs, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($urlRs, CURLOPT_SSL_VERIFYHOST, 0);
}
//发送消息,获取响应
$reponse = curl_exec($urlRs);
//关闭连接
curl_close($urlRs);
return $reponse;
}
}
log.php文件源码请查看之前的PHP错误排查生成日志文件的文章,连接:https://www.yj521.com/article/39.html
调用demo.php文件后生成的日志数据如下
GET提交接收数据:[2020-05-27 11:11:01][DEBUG]=>Array
(
[id] => 1
[name] => 越加网
)
// 文件参数
[2020-05-27 11:11:01][DEBUG]=>Array
(
)
POST提交接收数据-附带单文件上传:[2020-05-27 11:12:46][DEBUG]=>Array
(
[id] => 1
[name] => 越加网
)
[2020-05-27 11:12:46][DEBUG]=>Array
(
[headimg] => Array
(
[name] => 1.jpg
[type] => image/jpeg
[tmp_name] => C:\Windows\Temp\phpD080.tmp
[error] => 0
[size] => 121492
)
)
POST提交接收数据-附带多文件上传:[2020-05-27 11:13:29][DEBUG]=>Array
(
[id] => 1
[name] => 越加网
)
[2020-05-27 11:13:29][DEBUG]=>Array
(
[headimg0] => Array
(
[name] => 1.jpg
[type] => image/jpeg
[tmp_name] => C:\Windows\Temp\php7ACA.tmp
[error] => 0
[size] => 121492
)
[headimg1] => Array
(
[name] => 2.jpg
[type] => image/jpeg
[tmp_name] => C:\Windows\Temp\php7ACB.tmp
[error] => 0
[size] => 1954
)
)
源码下载链接:https://pan.baidu.com/s/1q5etgUKY4vSQ14aa2ymZlg
提取码:3lj3
压缩包解压密码:www.yj521.com
压缩包内容:
phpcurl.class
yj521.com.txt:使用说明文档
越加网:越加网快捷方式(给个支持^_^)