php 通过curl上传图片

通过curl上传图片

  • PHP < 5.5:
    使用 目前使用的php版本 7.1 测试无法使用 前面加@ 的方法上传文件 ,查找资料 使用 curl_setopt($ch,CURLOPT_SAFE_UPLOAD,FALSE) 可以解决,但是经测试 这种方式不行,显示的CURLOPT_SAFE_UPLOAD这个选项在该版本php中已经被废弃

  • 可以通过检测 有没有 curl_file_create 这个函数 也可以检测 有没有类\CURLFile class_exists('\CURLFile')

$filename = new \CURLFile(realpath($filepath),$minetype,$basename);
或者
$filename = curl_file_create(realpath($filepath),$minetype,$basename);

if (!function_exists('curl_file_create')) {
     function curl_file_create($filename, $mimetype = '', $postname = '') {
         return "@$filename;filename="
             . ($postname ?: basename($filename))
             . ($mimetype ? ";type=$mimetype" : '');
     }
 }

$ch = curl_init();
$filename = 'C:/Users/shanghai/Pictures/Camera Roll/23.jpg';
$minetype = 'image/jpeg';
$curl_file = curl_file_create($filename,$minetype);
$postData = [
    'file' => '111',
    'text' => '666',
    'file_name'=>$curl_file ,
];


curl_setopt($ch, CURLOPT_URL, 'xxx.com/test/curl');
//curl结果不直接输出
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//发送post  请求
curl_setopt($ch, CURLOPT_POST, 1);
// urlencoded 后的字符串,类似'para1=val1&para2=val2&...',也可以使用一个以字段名为键值,字段数据为值的数组 ,测试当值为数组时候  Content-Type头将会被设置成multipart/form-data 否则Content-Type 头会设置为 application/x-www-form-urlencoded 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
//curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));

//允许 cURL 函数执行的最长秒数
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
//不输出header 头信息
curl_setopt($ch, CURLOPT_HEADER,0);
//不验证证书 信任任何证书
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// 检查证书中是否设置域名,0不验证 0:不检查通用名称(CN)属性
1:检查通用名称属性是否存在
2:检查通用名称是否存在,是否与服务器的主机名称匹配
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
//设置 在HTTP请求中包含一个"User-Agent: "头的字符串
//curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);


$res = curl_exec($ch);
$error_no = curl_errno($ch);
$info = curl_getinfo($ch);
$err_msg = '';
if ($error_no) {
    $err_msg = curl_error($ch);
} else {
    print_r($res);
    dump($info);
}
curl_close($ch);

注意:

  • curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); $postData为数组时Content-Type 头部会被设置为 multipart/form-data ,通过file_get_contents('php://input')获取不到post提交的到数据,通过$_POST 可以,$_FILES 可以获取数据

image

$ch = curl_init();
$curl_file1 = curl_file_create('C:/Users/shanghai/Pictures/Camera Roll/23.jpg', 'image/png', pathinfo('C:/Users/shanghai/Pictures/Camera Roll/23.jpg',PATHINFO_BASENAME));
$curl_file2 = curl_file_create('C:/Users/shanghai/Pictures/Camera Roll/66tyr.jpg', 'image/png', pathinfo('C:/Users/shanghai/Pictures/Camera Roll/66tyr.jpg',PATHINFO_BASENAME));

$postData = [
    'file' => '111',
    'text' => '666',
    'file_name[0]'=>$curl_file1,
    'file_name[1]'=>$curl_file2,
    // 'file_name'=>$curl_file1 ,
];
curl_setopt($ch, CURLOPT_URL, 'xxx.com/test/curl');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_POST, 1);
// curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($postData));
// curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));

curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);


$res = curl_exec($ch);
$error_no = curl_errno($ch);
$info = curl_getinfo($ch);
$err_msg = '';
if ($error_no) {
    $err_msg = curl_error($ch);
} else {
    print_r($res);
    // dump($info);
}
curl_close($ch);


public function actionCurl()
{
    $request = Yii::$app->request;
    dump(file_get_contents('php://input'),$_FILES,$request->post(),$request->getContentType(),$request->getMethod());
}
  • curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData)); 这个参数值为urlencoded 后的字符串,类似'para1=val1&para2=val2&...'Content-Type 头部会被设置为 application/x-www-form-urlencoded ,通过file_get_contents('php://input')可以获取到post提交的到数据,通过$_POST 也可以,$_FILES 获取不到数据
    image

  • 传递json参数

$jsonData = '{"name":"xp","age":"18","sex":"男"}';
$postData = [
    'file' => '111',
    'text' => '666',
   'file_name[0]'=>$curl_file1,
   'file_name[1]'=>$curl_file2,
    // 'file_name'=>$curl_file1 ,
];
$header = [
   'Content-Type: application/json',
   'Content-Length:'.strlen($jsonData),
];
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
// 设置Content-Type: application/json,传递数组数据时, 都获取不到数据,文件也获取不到
//curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
//当不设置header 时 
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
  1. 设置Content-Type: application/json,传递json数据参数时,file_get_contents('php://input')可以获取到数据,其他获取不到
    image

  2. 不设置Content-Type: application/json,传递json数据参数时,传递file_get_contents('php://input')可以获取到数据,$_FILES获取不到 ,$_POST 获取到的是["{"name":"xp","age":"18","sex":"男"}" => ""]
    image

  3. 设置Content-Type: application/json,传递数组参数时,获取不到参数
    image

  • curl 多图上传时 ,传递的数组要加key
//像这样写可以获取到多图
$postData = [
    'file' => '111',
    'text' => '666',
   'file_name[0]'=>$curl_file1,
   'file_name[1]'=>$curl_file2,
    // 'file_name'=>$curl_file1 ,
];

//这样写的话 后面的会覆盖前面的
$postData = [
    'file' => '111',
    'text' => '666',
   'file_name[]'=>$curl_file1,
   'file_name[]'=>$curl_file2,
    // 'file_name'=>$curl_file1 ,
];
  • 只有当 Content-Type:application/x-www-form-urlencoded 时 php://input 和$_POST都有数据;当Content-Type:multipart/form-data 时 php://input 获取不到数据,$_FILES 可以获取上传图片的信息,$_POST也有数据;当Content-Type:application/json 并且传递的是json 数据时 , php://input可以正常获取json 数据,$_POST获取不到;当Content-Type:text/xml php://input可以正常获取xml 数据,$_POST获取不到

参考:

posted @ 2017-12-15 11:12  哦先生  阅读(12876)  评论(0编辑  收藏  举报