PHP Curl Put multipart

时间:2015-06-12 06:02:47

标签: php curl

我需要帮助将以下CURL命令转换为PHP。

curl -X PUT -F file=@myfile.zip -F file_type=binary \
    "https://example.com/rest/api/path"

使用PHP 5.6,'file'=>'@'.$filename不是一个选项。

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码将您的请求转换为PHP:

// initialise the curl request
$request = curl_init('https://example.com/rest/api/path');

// send a file
curl_setopt($request, CURLOPT_POST, true);
curl_setopt(
    $request,
    CURLOPT_POSTFIELDS,
    array(
      'file' => '@' . realpath('myfile.zip') .
      ';type='     . $_FILES['file']['type']
));

// output the response
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($request);

// close the session
curl_close($request);

更新5.6

// initialise the curl request
$request = curl_init('https://example.com/rest/api/path');

// send a file
curl_setopt($request, CURLOPT_POST, true);
curl_setopt(
    $request,
    CURLOPT_POSTFIELDS, new CurlFile('myfile.zip', $_FILES['file']['type'], 'myfile.zip'));

// output the response
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($request);

// close the session
curl_close($request);