Reputation: 163
I can't find the right format to post an update in Twitter, It's seems to obvious that no one explain it.
I'm Using PHP and a http_socket_oauth lib from http://www.neilcrookes.com/2010/04/12/cakephp-oauth-extension-to-httpsocket/
The really hard part for me is the 'media[]' parameter, I don't know exactly where to put it, and how it should be written. I've try to many things but always get the same error:
{"errors":[{"code":195,"message":"Missing or invalid url parameter."}]}
This is the request what I think must be ok, but it isn't:
$request = array(
'method' => 'POST',
'header' => array(
'Content-Type' => 'multipart/form-data',
),
'uri' => array(
'host' => 'api.twitter.com',
'path' => '1.1/statuses/update_with_media.json',
'scheme' => 'https',
),
'auth' => array(
'method' => 'OAuth',
'oauth_token' => $this->Session->read('Twitter.oauth_token'), // From session
'oauth_token_secret' => $this->Session->read('Twitter.oauth_token_secret'), // From session
'oauth_consumer_key' => Configure::read('Twitter.consumer_key'), // APP key
'oauth_consumer_secret' => Configure::read('Twitter.consumer_secret'), // APP secret
),
'body' => array(
'status' => 'This is a Test ',
'media[]' => '@{http://www.mydomail.com/img/Drop100.gif},
),
);
Thanks!
Upvotes: 0
Views: 190
Reputation: 163
I have solved the issue.
The problem is that the http_socket_oauth lib from http://www.neilcrookes.com/2010/04/12/cakephp-oauth-extension-to-httpsocket/ doesn't prepare data for multipart form. So I add the handle for Multipart just before sending the request.
The Request from my controller is built this way:
$request = array(
'method' => 'POST',
'header' => array(
'Content-Type' => 'multipart/form-data',
),
'uri' => array(
'host' => 'api.twitter.com',
'path' => '1.1/statuses/update_with_media.json',
'scheme' => 'https',
),
'auth' => array(
'method' => 'OAuth',
'oauth_token' => $this->Session->read('Twitter.oauth_token'), // From Session
'oauth_token_secret' => $this->Session->read('Twitter.oauth_token_secret'), // From Session
'oauth_consumer_key' => Configure::read('Twitter.consumer_key'),
'oauth_consumer_secret' => Configure::read('Twitter.consumer_secret'),
),
'data' => array(
'media[]' => './img'.DS.'Dropial-logo-128.png',
),
'body' => array(
'status' => 'This is a test!',
),
);
The code I added to the http_socket_oauth lib is:
if(isset($request['header']['Content-Type']) && $request['header']['Content-Type']=='multipart/form-data'){
App::uses('String', 'Utility');
$boundary = String::uuid();
$body = "--{$boundary}\r\n";
foreach ($request['body'] as $key => $value) {
$body .= "Content-Disposition: form-data; name=\"{$key}\"\r\n";
$body .= "\r\n";
$body .= "{$value}\r\n";
$body .= "--{$boundary}\r\n";
}
foreach ($request['data'] as $key => $path) {
$body .= "Content-Disposition: form-data; name=\"{$key}\"; filename=\"{$path}\"\r\n";
$body .= "\r\n";
$body .= file_get_contents($path) . "\r\n";
$body .= "--{$boundary}--\r\n";
}
$request['body'] = $body;
$request['header']['Content-Type'] = "multipart/form-data; boundary={$boundary}";
}
As you can see I had to rewrite the body and header param.
This is just before
return parent::request($request);
That's it. Thank you anyway Terence!
Upvotes: 1