如何从windows os中通过cURL从php发送文件夹中的所有文件?

时间:2018-04-27 02:28:14

标签: php curl

我想通过curl发送文件夹中的所有文件。我已经尝试过curl发送的单个文件,但我需要通过一个curl post请求发送所有文件,其中包含一些文本,如

username: admin
password: 123456
file1: file_path1
file2: file_path2
fiit3: file_path3

是否可以通过单个curl请求发送包含某些文本字段的所有文件?如果可能的话,请给我发送用于通过curl发送多个文件的示例代码。

我的代码

$api       = "http://localhost/curl/file.php";  // API to Image
$username  = "project";                         // Server Access Username
$password  = "aiub@2018";                       // Server Access Password
$device_id = 1001;                              // Device ID
$file_path = "C:/Users/Himel/Pictures";         // Store Image Path

$file_list = glob($file_path."/*");
$post_data = array(
    'username'  => $username,
    'password'  => $password,
    'device_id' => $device_id
);

foreach ($file_list as $file) {
    $post_data['files[]'] = curl_file_create($file);
}

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL             => $api,
    CURLOPT_RETURNTRANSFER  => true,
    CURLOPT_ENCODING        => "",
    CURLOPT_MAXREDIRS       => 10,
    CURLOPT_TIMEOUT         => 30,
    CURLOPT_HTTP_VERSION    => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST   => "POST",
    CURLOPT_POSTFIELDS      => $post_data,
    CURLOPT_HTTPHEADER      => array(
        "cache-control: no-cache",
        "content-type: multipart/form-data;"
    ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

echo $response;

0 个答案:

没有答案
相关问题