[英]PHP Curl Put multipart
I need help converting following CURL command to PHP. 我需要将以下CURL命令转换为PHP的帮助。
curl -X PUT -F file=@myfile.zip -F file_type=binary \
"https://example.com/rest/api/path"
Using PHP 5.6 so 'file'=>'@'.$filename
is not an option. 使用PHP 5.6,因此不能使用'file'=>'@'.$filename
。
You can use following code to convert your request in to PHP: 您可以使用以下代码将您的请求转换为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);
UPDATE FOR 5.6 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.