如何在php中使用curl发送“ file”参数?
这是API参考:
http --form POST "http://b24.sigurd.bx/rest/upload.json?auth=n2423m863oil59f99c9g0bm4918l5erz&token=disk%7CaWQ9Mjg5Jl89QkYzazEzaXNnUjNHcVZQcDJZaGxGRmI4TGhXOG5EZXQ%3D%7CInVwbG9hZHxkaXNrfGFXUTlNamc1Smw4OVFrWXp
hekV6YVhOblVqTkhjVlpRY0RKWmFHeEdSbUk0VEdoWE9HNUVaWFE9fG4yNDIzbTg2M29pbDU5Zjk5YzlnMGJtNDkxOGw1ZXJ6Ig%3D%3D.Aga709nyY0%2BrFiv3laHjfg6XuOO5JT6ttjU%2F53ifphM%3D" file@~/somelongfile.log
我有此代码:
$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . $filename;
$eol = "\r\n";
$data = '';
$mime_boundary='----WebKitFormBoundary'.md5(time());
$data .= '--' . $mime_boundary . $eol;
$data .= 'Content-Disposition: form-data; name="file"; filename="' . $uploadfile . '"' . $eol;
$data .= 'Content-Type: ' . $filetype . $eol .$eol.$eol;
$data .= '--' . $mime_boundary . "--";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => array(
": ",
"Content-Type: application/x-www-form-urlencoded",
"Postman-Token: 84f75cb8-6548-420e-ad9b-fb72f40661d2",
"cache-control: no-cache",
"content-type: multipart/form-data; boundary=$mime_boundary"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
if ($err) {
curl_close($curl);
return "cURL Error #:" . $err;
}
else{
curl_close($curl);
return json_decode($response);
}
但我总是收到此回复:
{\"error\":\"DISK_BASE_SERVICE_22001\",\"error_description\":\"Error: required parameter file (DISK_BASE_SERVICE_22001).\"}
使用邮递员,它可以工作并生成以下php curl代码:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://b24-fyp52c.bitrix24.de/rest/upload.json?auth=29c79b5c002e73c2002e73c000000001e0e3035e6099cf36f62f3c1492d92b9ce7fdd8&token=disk%7CaWQ9ODcmZ2VuZXJhdGVVbmlxdWVOYW1lPTAmXz11bm5LcjRLN1lHYUp2UkRuWjZUU2ZTNjZQaVRwUFNETw%3D%3D%7CInVwbG9hZHxkaXNrfGFXUTlPRGNtWjJWdVpYSmhkR1ZWYm1seGRXVk9ZVzFsUFRBbVh6MTFibTVMY2pSTE4xbEhZVXAyVWtSdVdqWlVVMlpUTmpaUWFWUndVRk5FVHc9PXwyOWM3OWI1YzAwMmU3M2MyMDAyZTczYzAwMDAwMDAwMWUwZTMwMzVlNjA5OWNmMzZmNjJmM2MxNDkyZDkyYjljZTdmZGQ4Ig%3D%3D.u8CkPCbayq3JJ8QJqcAyDKN5EFFAvYmSrksFXcU8oVU%3D%0A",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"C:\\Users\\kenan\\Pictures\\Bank.jpeg\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
CURLOPT_HTTPHEADER => array(
": ",
"Content-Type: application/x-www-form-urlencoded",
"Postman-Token: 84f75cb8-6548-420e-ad9b-fb72f40661d2",
"cache-control: no-cache",
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
从我的代码到邮递员的代码,我看不出有什么区别,但无法正常工作,我们将不胜感激。
谢谢!
答案 0 :(得分:0)
使用CURLFile
对象。从文档中:
<?php
/* http://example.com/upload.php:
<?php var_dump($_FILES); ?>
*/
// Create a cURL handle
$ch = curl_init('http://example.com/upload.php');
// Create a CURLFile object
$cfile = new CURLFile('cats.jpg','image/jpeg','test_name');
// Assign POST data
$data = array('test_file' => $cfile);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// Execute the handle
curl_exec($ch);
?>
答案 1 :(得分:0)
我设法使它像这样工作:
exec ("curl -X POST -F 'file=@" . $uploadfile . "' '" . $url . "'");
有没有办法做到这一点,老式的卷曲方式?还是这是唯一的解决方案?
谢谢!