2

From bash terminal, I successfully executed the following command.

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:[email protected]/api/media

Now I want to create a PHP script that uses the phpcurl library to execute an equivalent command. My code below is shown, but it's not working. The http://johnuser:[email protected]/api/media server is giving me a generic error message. I'm pretty sure I'm making a mistake by not passing the right parameters or setting the right flags in my php code. Can anyone tell me what's wrong?

$url = 'http://johnuser:[email protected]/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);

The error message I got is {"status":{"message":"typeMismatch ","error":true,"code":500}}

9
  • That's the error message? "Generic error message"? Commented Dec 10, 2011 at 17:42
  • you might need this, curl_setopt(CURLOPT_VERBOSE, TRUE); Commented Dec 10, 2011 at 17:45
  • You could try taking the username/password out of the URL and doing curl_setopt($ch, CURLOPT_USERPWD, "johnuser:johnpass");, although I don't know if this will make a difference Commented Dec 10, 2011 at 17:48
  • the error message is really {"status":{"message":"typeMismatch ","error":true,"code":500}} Commented Dec 10, 2011 at 17:49
  • 1
    If that's your error message I would consult the API documentation of the service you're querying to see what code 500 means. An off the cuff guess seems like a 500 internal server error, which, if it were the case wouldn't be something you could help. Commented Dec 10, 2011 at 17:51

1 Answer 1

2

I think this is what you need to be doing, if you want upload a file:

// 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 file uploads are done with a MIME multipart message, using the multipart/form-data sub-type.

Sign up to request clarification or add additional context in comments.

1 Comment

yes, that worked. Except there's just one typing mistake...I need to add a period to $body = "--$boundary\r\n" to make it $body = "--$boundary\r\n"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.