loxpcw
Last Updated: February 25, 2016
·
4.337K
· euantor
Irnhmv6

Easily post to twitter via your PHP application

There will likely come a time when you feel the need to post a tweet to your twitter feed from within your custom PHP application- I know I have needed to do so a few times.

Luckily, there's a great library out there that's free to use thanks to Abraham Williams named twitteroauth.

Using twitteroauth is extremely easily - we need to simply include it in our project and provide our necessary Twitter API details (which you receive when you create a new app on Twitter. The necessary code should look a little something like this:

require_once 'libs/twitteroauth.php';

if (!defined('CONSUMER_KEY'))
{
    define('CONSUMER_KEY', '');
}

if (!defined('CONSUMER_SECRET'))
{
    define('CONSUMER_SECRET', '');
}

if (!defined('OAUTH_TOKEN'))
{
    define('OAUTH_TOKEN', '');
}

if (!defined('OAUTH_SECRET'))
{
    define('OAUTH_SECRET', '');
}

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET);
$content = $connection->get('account/verify_credentials');

$connection->post('statuses/update', array('status' => 'My new status update!'));

Pretty simple eh?

Say Thanks
Respond