简体   繁体   English

PHP-cURL文件上传

[英]PHP - cURL file upload

I'm writing a script to uploading images to a specific api. 我正在编写一个脚本,用于将图像上传到特定的api。 Uploading one image is not a problem but more than one. 上载一张图像不是问题,而是多个。 The API documentation says the following: API文档说明以下内容:

curl -v -s -u username:password \
-H "Content-Type: multipart/form-data" \
-H "Accept: application/vnd.de.xxx.api+json" \
-F "image=@img1.jpeg;type=image/jpeg" \
-F "image=@img2.jpeg;type=image/jpeg" \
-XPUT 'https://services.xxx.de/seller-api/sellers/123/ads/123/images'

My script: 我的剧本:

$ch = curl_init();

$images = array(
    'image' => new CURLFile('PATH\1.jpg', 'image/jpeg', 'image')
);

curl_setopt($ch, CURLOPT_URL, 'https://services.xxx.de/seller-api/sellers/123/ads/123/images');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $images);
curl_setopt($ch, CURLOPT_USERPWD, 'USERNAME:PASSWORD');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Host: services.xxx.de',
    'Content-Type: multipart/form-data',
    'Accept: application/vnd.de.xxx.api+json'
));
curl_setopt($ch, CURLINFO_HEADER_OUT, true);

The problem is that the api delete other images if I'm uploading a new one. 问题是,如果我要上传新的图像,则api会删除其他图像。 But I can't write an array with multiple same keys like that: 但是我不能用多个相同的键写一个数组:

$images = array(
    'image' => new CURLFile('PATH\1.jpg', 'image/jpeg', 'image'),
    'image' => new CURLFile('PATH\2.jpg', 'image/jpeg', 'image')
);

So the question is how I can set multiple CURLFiles with the same index like the curl command in the documentation? 所以问题是,如何像文档中的curl命令一样,使用相同的索引设置多个CURLFiles?

Working at mobile seller api ;-) Here's the solution: 1. create an array of image-files like this one: 在移动卖家API上工作;-)这是解决方案:1.创建一个图像文件数组,如下所示:

$count=0;
$files['image'.$count] = curl_file_create(#a#, 'image/jpeg', #b#)
$count++;

-> #a# full qualified path to your file on your server -> #b# optionally a name for the image in the form 'image'.$count ->#a#服务器上文件的完整限定路径->#b#(可选)以“图像”形式表示图像的名称。

Important: the array-key MUST be namend exactly in this way while you can name your array as you want ;-) 重要提示:必须以这种方式准确地命名阵列键,同时您可以根据需要命名阵列;-)

  1. Options for curl: - remove the "host" from your HTTP_HEADER and add CURLOPT_CONNECTTIMEOUT with a value in seconds ;-) curl的选项:-从HTTP_HEADER中删除“主机”,并以秒为单位添加CURLOPT_CONNECTTIMEOUT ;-)

By the way, in all actual php versions, we use TRUE and FALSE instead of 0 and 1 as values for curl-options. 顺便说一下,在所有实际的php版本中,我们使用TRUE和FALSE而不是0和1作为curl-options的值。

It works for me ... 这个对我有用 ...

My experience after hours of searching: With the official curl function, you can not make an multiple-upload of images. 经过数小时的搜索后的经验:使用官方的卷曲功能,您无法进行多次上传图像。

You can only upload multiple images using the shell command. 您只能使用shell命令上传多张图像。 Solution: Extend the shell script to include all images to be uploaded, then "shell_exec ($ str)" or exec ($ str). 解决方案:扩展外壳程序脚本以包含所有要上传的图像,然后是“ shell_exec($ str)”或exec($ str)。 For me this works. 对我来说,这有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM