从手动到任何web服务,我需要在PHP中构造代码。我有测试环境,我知道当我发布一张图片时,效果很好;但是当我需要发布更多图像时,它无法正常工作。在手册中是:
curl -v -s -u username:password \
-H "Content-Type: multipart/form-data" \
-H "Accept: application/vnd.de.mobile.api+json" \
-F "image=@img1.jpeg;type=image/jpeg" \
-F "image=@img2.jpeg;type=image/jpeg" \
-XPUT 'https://www.example.com/seller-api/sellers/12/ads/217221/images'
以下是适用于一个文件上传的代码:
$sql[2] = "SELECT * FROM table WHERE car_id='".$car['id']."'";
$result[2] = mysql_query($sql[2]);
if(mysql_num_rows($result[2])>0){
$request = curl_init();
curl_setopt($request, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($request, CURLOPT_PROXY, $proxy);
curl_setopt($request, CURLOPT_USERPWD, $user . ":" . $pass);
curl_setopt($request, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($request, CURLOPT_RETURNTRANSFER, TRUE);
while($img = mysql_fetch_array($result[2])){
$file = '@inzerce/'.$img['nazev'];
$post['image'] = $file.";type=image/jpeg";
}
curl_setopt($request, CURLOPT_POSTFIELDS, $post);
curl_setopt($request, CURLOPT_URL, $url);
curl_setopt($request, CURLOPT_HTTPHEADER, array('Host: services.mobile.de','Content-type: multipart/form-data; boundary=vjrLeiXjJaWiU0JzZkUPO1rMcE2HQ-n7XsSx', 'Accept: application/vnd.com.example.api+json'));
curl_setopt($request, CURLOPT_HEADER, TRUE);
curl_setopt($request, CURLINFO_HEADER_OUT, TRUE);
$result2 = curl_exec($request);
$info2 = curl_getinfo($request);
curl_close($request);
}
但是当我需要上传更多文件时,只有最后一个文件上传到服务器。当我尝试将图像插入数组时(例如:$ post [$ img [' id']] [' image']),此文件不接受的服务器。我尝试将$ post [' image']重写为$ post [' image'。$ img [' id']],但它不起作用无论是。我在bash中尝试过它,而不是图像不起作用。
有人可以帮忙吗?
答案 0 :(得分:0)
您必须在数组中传递它,这是快速示例
<?php
//Initialise the cURL var
$ch = curl_init();
//Get the response from cURL
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
//Set the Url
curl_setopt($ch, CURLOPT_URL, 'http://s/');
//Create a POST array with the file in it
$postData = array(
'testData[0]' => new \CURLFile( __FILE__ ),
'testData[1]' => new \CURLFile( __FILE__ ),
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
// Execute the request
$response = curl_exec($ch);
var_dump($response);