2017-08-01 69 views
0

我正在編寫一個腳本來上傳圖片到一個特定的api。上傳一張圖片不是問題,但不止一個。該API文檔說以下內容:PHP - cURL文件上傳

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

我的腳本:

$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); 

的問題是,API刪除其它影像,如果我上傳一個新的。但我不能寫有多個相同的密鑰像一個數組:

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

所以,問題是我怎麼可以設置多個CURLFiles與像文檔中的curl命令相同的索引?

+0

看到這個職位https://stackoverflow.com/questions/44474192/upload-multiple-files-with-curl,或許它會幫助你。 –

+0

不,不幸的是,不是因爲api迴應:{「errors」:[{「key」:「unsupported-form-element」}]}這意味着cURL文件的關鍵字必須是「image」,而不是其他。 ..但非常感謝你! –

+0

然後在循環中嘗試您的Curl調用。 –

回答

-1

我經過幾個小時的搜索之後的體驗: 使用官方捲曲功能,您不能進行多次上傳圖像。

您只能使用shell命令上傳多個圖像。 解決方案:擴展shell腳本以包含要上傳的所有圖像,然後是「shell_exec($ str)」或exec($ str)。對我來說這是有效的。

0

在移動賣家API ;-) 在這裏工作是解決方案:1。 創建圖像文件的這樣一個數組:

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

- 上>#A#完全合格的路徑到您的文件你服務器 - >#b#可選地以'image'形式表示圖像的名稱$ count

重要提示:array-key必須以這種方式正確命名,而您可以根據需要命名陣列;-)

    對於捲曲個
  1. 選項: - 從你的HTTP_HEADER 刪除「主機」,並與在幾秒鐘;-)

值添加CURLOPT_CONNECTTIMEOUT順便說一句,在所有實際的PHP版本中,我們使用的真假,而不是0和1作爲捲曲選項的值。

它爲我...