Help with implementing Twitter on a website

Gordon.C

Member
Hi,

I am getting kind of desperate for answers.

I am using this ridiculously simple script to auto Tweet on behalf of a user.

PHP:
$image = 'test.jpg';

// Insert your keys/tokens
$consumerKey = 'xxx';
$consumerSecret = 'xxx';
$OAuthToken = 'xxx';
$OAuthSecret = 'xxx';

// Full path to twitterOAuth.php (change OAuth to your own path)
require_once('connect/twitter/twitteroauth.php');

$params = array(
    'status' => $_REQUEST['content'],
    'media[]' => '@{$image}'
);

// create new instance
$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $OAuthToken, $OAuthSecret);

// Send tweet
$tweet->post('statuses/update', $params);

Upon executing this code, text gets Tweeted on my twitter account however the image does not. I have been previously advised to only attempt tweeting local files and I was advised this is the proper way to specify the media[].

Why isnt this posting the image while its posting the text to twitter?

Can anybody please help? I have been struggling with this for days
 
Sorry I'm not real familiar with this API, before I download all the components and try it myself, have you tried fully qualifying the path to the image? Or otherwise made sure that the script can actually access it?
 
Sorry I'm not real familiar with this API, before I download all the components and try it myself, have you tried fully qualifying the path to the image? Or otherwise made sure that the script can actually access it?

Yes I tried several variants of paths to the file. I also tried submitting the file in binary through file_get_contents. That did not help either.

If you could get this or any other code to work, that would be fantastic because I am running out of ideas.
 
Hi,

have you had any luck so far? I think I finally got to work the same thing but with Facebook, Twitter still very little luck.
 
I've gotten everything pulled to my VM. Set up an application in twitter so I have consumerkeys. haven't gotten much to happen yet. Had a busy weekend, hopefully will have more time over the next couple days.
 
From what I can gather, this is the way it should work.

It takes a bit longer to run, but I haven't seen a tweet yet. Like you, I can do the text only no problem.

PHP:
$imageFilename = 'test.jpg';
$imageRAW = fopen($imageFilename, "rb");
$image = fread($imageRAW, filesize($imageFilename));
fclose($imageRAW);

// insert your keys/tokens
$consumerKey = '';
$consumerSecret = '';
$OAuthToken = '';
$OAuthSecret = '';

// Full path to twitterOAuth.php (change OAuth to your own path)
require_once('twitteroauth/twitteroauth.php');

$params = array(
        'status' => "test scripted picutre add",
        'media[]' => "{$image};type=image/jpg;filename={$imageFilename}"
);

$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $OAuthToken, $OAuthSecret);

$tweet->post('statuses/update_with_media', $params);
 
Did some more digging. Try this fork of the library. https://github.com/natefanaro/twitteroauth

Code is mostly the same. Basically change post to upload.
PHP:
$imageFilename = '/home/dave/DSC00999.jpg';
$imageRAW = fopen($imageFilename, "rb");
$image = fread($imageRAW, filesize($imageFilename));
fclose($imageRAW);

// insert your keys/tokens
$consumerKey = '';
$consumerSecret = '';
$OAuthToken = '';
$OAuthSecret = '';

// Full path to twitterOAuth.php (change OAuth to your own path)
require_once('twitteroauth/twitteroauth.php');

$params = array(
        'status' => "test scripted picutre add",
        'media[]' => "{$image};type=image/jpg;filename={$imageFilename}"
);

$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $OAuthToken, $OAuthSecret);

$tweet->upload('statuses/update_with_media', $params);

Works for me :)
 
Last edited:
Did some more digging. Try this fork of the library. https://github.com/natefanaro/twitteroauth

Code is mostly the same. Basically change post to upload.
PHP:
$imageFilename = '/home/dave/DSC00999.jpg';
$imageRAW = fopen($imageFilename, "rb");
$image = fread($imageRAW, filesize($imageFilename));
fclose($imageRAW);

// insert your keys/tokens
$consumerKey = '';
$consumerSecret = '';
$OAuthToken = '';
$OAuthSecret = '';

// Full path to twitterOAuth.php (change OAuth to your own path)
require_once('twitteroauth/twitteroauth.php');

$params = array(
        'status' => "test scripted picutre add",
        'media[]' => "{$image};type=image/jpg;filename={$imageFilename}"
);

$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $OAuthToken, $OAuthSecret);

$tweet->upload('statuses/update_with_media', $params);

Works for me :)

Thank you so much for help. This is finally the first working solution for me.
 
Back
Top