auto-tweet-from-php

Codebird is PHP based library which enables to post tweet directly from your PHP website. The tutorial is purely for beginners if you need further customization and advance integration i suggest you to go through through documentation at CodeBird Repo.

Create a twitter applications

To post auto-tweets from your PHP website you need to setup a twitter application and use the details in PHP file in order to authenticate your account.

create twitter application

Once you create an application, click on settings to generate access token which’ll be used in PHP file later. Make sure to change the permission of the application to READ/WRITE instead of read only.

twitter-app-settings

You need to copy the following fields carefully

  1. Consumer Key (API)
  2. Consumer Secret (API Secret)
  3. Access Token
  4. Access Token Secret

Once you finish all the configuration in twitter app. Download two files (codebird.php and cacert.pem) from CodeBirth repository and upload in your root or any suitable location according to your directory structure.

Customization of your PHP file

Now authenticate your application using the details above. First of all include codebird.php and create the file as follow

<?php
require_once ('codebird.php');
\Codebird\Codebird::setConsumerKey('YOURKEY', 'YOURSECRET');
$cb = \Codebird\Codebird::getInstance();
$cb->setToken('YOURTOKEN', 'YOURTOKENSECRET');
$cb->statuses_update('status=Wow my first tweet from PHP website! Warka Pe Tak..');
?>

Thats it! If you have any question feel free to ask in comments. The code can be customize according to your needs. It can be a media upload tweet, a twitter card or just a simple tweet. The $cb->statues_update can be fetch from a database or variable any time.

Make sure the length of your post is not max than 140 character as per twitter policy. You can use substr PHP function to trim your title, include hashtag and URL for best result.

Comments

comments

Comments closed