David Ogilo

menu search

PECL OAuth and multipart

When trying to call the twitter media upload resource using PECL OAuth, I noticed the API call was failing. I later found out that the reason was that the wrong content-type was sent. The header contained Content-Type: application/x-www-form-urlencoded instead of the required Content-Type: multipart/formdata.

To set the content type to multipart when using PECL OAuth, the request engine needs to be set to use cURL instead of PHP Streams as multipart is only supported by the former. Also if sending a media file, @ needs to be prepended to both the file and parameter name.

$oauth = new OAuth( $consumerKey, $consumerSecret );
$oauth->setRequestEngine( OAUTH_REQENGINE_CURL );
$oauth->setToken( $token, $secret );
 
$status = 'This is a test';
$image = './test.jpg';
 
$oauth->fetch( 'https://upload.twitter.com/1/statuses/update_with_media.json', array( '@media[]' => '@' . $image, 'status' => $status ), OAUTH_HTTP_METHOD_POST );
Tags: , ,

Old school browser detected!

Your browser is out of date, which means you can't use any of the site's required features. You can either upgrade your browser or switch to either Mozilla Firefox or Google Chrome