CURL - php vs命令行bash

时间:2011-12-10 17:30:01

标签: php curl

从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

现在我想创建一个使用phpcurl库执行等效命令的PHP脚本。我的代码显示如下,但它不起作用。 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}}

1 个答案:

答案 0 :(得分:2)

我认为如果你想上传文件,这就是你需要做的事情:

// 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文件上传使用multipart/form-data子类型MIME multipart message完成。

相关问题