CURL - php vs command line bash


CURL - php vs command line bash

在bash终端中,我成功地执行了以下命令:

curl -v -L -F file='@/var/www/dev/public_html/sixties.mov' -F title='my video' -F description='this is a video' -F language='eng' -F license='a2be14e1-37d9-11dd-ae16-0800200c9a66' -F country='US' http://johnuser:johnpass@website.com/api/media

现在我想创建一个PHP脚本,它使用phpcurl库来执行一个等效的命令。下面显示了我的代码,但它不起作用。http://johnuser:johnpass@website.com/api/media服务器给我一个通用的错误信息。我很确定我犯了一个错误,没有传递正确的参数或设置正确的标志在我的php代码。谁能告诉我怎么了?

$url = 'http://johnuser:johnpass@website.com/api/media';
$fields = array();
$fields['file'] = '@/var/www/dev/public_html/sixties.mov';
$fields['title'] = 'my video';
$fields['description'] = 'this is a test';
$fields['language'] = 'eng';
$fields['country'] = 'US';
$fields['license'] = 'a2be14e1-37d9-11dd-ae16-0800200c9a66';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
print_r($result);

我得到的错误信息是{"status":{"message":"typeMismatch ","error":true,"code":500}}

我认为这是你需要做的,如果你想上传一个文件:

// Parameters
$url = 'http://website.com/api/media';
$username = 'johnuser';
$password = 'johnpass';
$upload_file = '/var/www/dev/public_html/sixties.mov';
// Declare a couple of arrays we will need
$fields = $headers = array();
// Standard POST fields
$fields['title'] = 'my video';
$fields['description'] = 'this is a test';
$fields['language'] = 'eng';
$fields['country'] = 'US';
$fields['license'] = 'a2be14e1-37d9-11dd-ae16-0800200c9a66';
// Boundary string for multipart message
$boundary = '--=-=-'.md5(uniqid()).rand().'-=-=--';
// Start the body with the file to be uploaded
$body = "--$boundary'r'n"
      . "Content-Disposition: form-data; name='"file'"; filename='"".basename($upload_file)."'"'r'n"
      . "Content-Type: application/octet-stream'r'n" // You should put the right MIME type here
      . "'r'n"
      . file_get_contents($upload_file) . "'r'n";
// Loop the fields and build the rest of the body
foreach ($fields as $name => $value) {
  $body .= "--$boundary'r'n"
         . "Content-Disposition: form-data; name='"$name'"'r'n"
         . "'r'n"
         . "$value'r'n";
}
// Finish the body
$body .= "--$boundary--";
// Add a couple of headers
$headers[] = "Content-Type: multipart/form-data; boundary='"$boundary'"";
$headers[] = 'Content-Length: ' . strlen($body);
$ch = curl_init();
// Set the cURL options
curl_setopt($ch, CURLOPT_URL,            $url);
curl_setopt($ch, CURLOPT_USERPWD,        "$username:$password");
curl_setopt($ch, CURLOPT_POST,           TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS,     $body);
curl_setopt($ch, CURLOPT_HTTPHEADER,     $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
print_r($result);

POST文件上传是通过MIME多部分消息完成的,使用multipart/form-data子类型。