Reputation: 323
please help me i have a problem uploading image to twitter using api v1.1 it was working perfect one month ago but it now doesn't work. i'm using Abraham twitteroauth https://github.com/abraham/twitteroauth and i'm using enctype="multipart/form-data" as attribute in the form tag this is my code :
require 'inc/twitter.class.php';
if(!empty($_FILES['media']['name'])){ // tweet with media
$file_name = $_FILES['media']['name'];
$file_type = $_FILES['media']['type'];
$file_size = $_FILES['media']['size'];
$file_temp = $_FILES['media']['tmp_name'];
$file_error = $_FILES['media']['error'];
$handle = fopen($file_temp,'rb');
$image = fread($handle,filesize($file_temp));
fclose($handle);
$tweet = new TwitterOAuth($YOUR_CONSUMER_KEY, $YOUR_CONSUMER_SECRET, $row_user_info['access_token_oauth_token'], $row_user_info['access_token_oauth_token_secret']);
$result = $tweet->post('statuses/update_with_media',array('media[]' => "@{$image};type={$file_type};filename={$file_temp}",'status' => 'test'));
echo '<pre>';
print_r($result);
echo '</pre>';
the result is:
stdClass Object
(
[errors] => Array
(
[0] => stdClass Object
(
[code] => 195
[message] => Missing or invalid url parameter.
)
)
)
i don't know what is missing in that please help me
Upvotes: 3
Views: 4168
Reputation: 323
thanks very much but i edit the code and works fine now
if (!empty($_FILES)) {
// we set the type and filename are set here as well
$handle = fopen($_FILES['image']['tmp_name'],'rb');
$image = fread($handle,filesize($_FILES['image']['tmp_name']));
fclose($handle);
$params = array(
'media[]' => "@{$image};type={$_FILES['image']['type']};filename={$_FILES['image']['name']}",
'status' => $_POST['status']
);
$twitteroauth = new TwitterOAuth($YOUR_CONSUMER_KEY, $YOUR_CONSUMER_SECRET, $row_user_info['access_token_oauth_token'], $row_user_info['access_token_oauth_token_secret']);
$dd = $twitteroauth->post('statuses/update_with_media',$params,true);
var_dump($dd);
}
Upvotes: 0
Reputation: 1539
this work for me
---html---
<form action="" method="POST" enctype="multipart/form-data">
<div>
<label for="status">Tweet Text</label>
<textarea type="text" name="status" rows="5" cols="60"></textarea>
<br />
<label for="image">Photo</label>
<input type="file" name="image" />
<br />
<input type="submit" value="Submit" />
</div>
</form>
---php---
if (!empty($_FILES)) {
// we set the type and filename are set here as well
$params = array(
'media[]' => "@{$_FILES['image']['tmp_name']};type={$_FILES['image']['type']};filename={$_FILES['image']['name']}",
'status' => $_POST['status']
);
$dd = $twitteroauth->post('statuses/update_with_media',$params,true);
var_dump($dd);
}
use this modified library
Upvotes: 1
Reputation: 1054
It was an issue with Abraham twitteroauth related to sending tweets with media. You can find fixed version of Twitteroauth here:
Using this library you have to call:
$result = $tweet->post('statuses/update_with_media',array('media[]' => "@{$image};type={$file_type};filename={$file_temp}",'status' => 'test'), true);
The last parameter true
means that you're sending multipart request.
Upvotes: 1