Auto-publish to Twitter

In this tutorial I will show you how to have your stories sent to the published page automatically Tweeted to Twitter using whatever Twitter account you want.

I have already shown you how to use Twitterfeed and Bit.ly to auto publish your published submissions, now lets cut out the middleman, Twitterfeed.

The following code was sent to us by Jorge from the good folks at Bierzeame.

Open your libs/link.php file and find:
function publish() {
if(!$this->read) $this->read_basic();
$this->published_date = time();

totals_adjust_count($this->status, -1);
totals_adjust_count('published', 1);

$this->status = 'published';
$this->store_basic();
}

change it to:
function publish() {
if(!$this->read) $this->read_basic();
$this->published_date = time();

totals_adjust_count($this->status, -1);
totals_adjust_count('published', 1);

$this->status = 'published';
$this->twitter();
$this->store_basic();
}

find:

function username() {

just before it add:
function twitter(){
global $db;

$id = $this->id;
if(!is_numeric($id)){return false;}
if(($link = $db->get_row("SELECT link_title FROM " . table_links . " WHERE link_id = $id"))) {

$message = $link->link_title;
$toshort = getmyFullurl("storyURL", $this->category_name(), urlencode($link->link_title_url), $id);

$bitly = "http://api.bit.ly/shorten?version=2.0.1&longUrl=".urlencode($toshort)."&login=BITLYUSERNAME&apiKey=YOURBITLYAPICODE&history=1";

$bitlyresponse = file_get_contents($bitly);

$jsonresponse = @json_decode($bitlyresponse,true);

$urlshort = $jsonresponse['results'][$toshort]['shortUrl'];

$messagelen = strlen($message);
$urllen = strlen($urlshort);
$totallen = $messagelen+$urllen+1;

if ($totallen>140){
$urllen = $urllen+4;
$message = substr($message,0,139-$urllen);
$message = $message."...";
}

$message = $message." ".$urlshort;

$args= 'status='.urlencode($message);

$twit = curl_init();
curl_setopt($twit, CURLOPT_URL, 'http://twitter.com/statuses/update.xml');
curl_setopt($twit, CURLOPT_VERBOSE, 0);
curl_setopt($twit, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($twit, CURLOPT_VERBOSE, 0); // no imprimir nada
curl_setopt($twit, CURLOPT_USERPWD, "TWITTERUSERNAME:TWITTERPASSWORD");
curl_setopt($twit, CURLOPT_POSTFIELDS, $args);
curl_setopt($twit, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($twit, CURLOPT_POST, 1);

$result = curl_exec($twit);
$http_status = curl_getinfo($twit, CURLINFO_HTTP_CODE);

curl_close($twit);

if( $http_status != "200" ) {
return false;
}

return true;
}
}

Save.

That’s it. Now every story that is sent to the published page will auto Tweet, giving your members another reason to submit.

Make sure you edit your Twitter login details and your Bit.ly details.

It will shorten the submission URL using Bit.ly and send the user when clicked on Twitter to your story page. This will also track how many clicks each one gets which you can check when you login to Bit.ly.

bitlyclicks

If you enjoyed this post, make sure you subscribe to my RSS feed!

  • Well, all you need is a computing language (like PHP) which can read in the RSS and then through the twitter API update a twitter account. You can find many of these types of classes by doing a search for things like "PHP twitter class" on Google.
  • They will give you a code that you have to send as a text to the number they provide depending where you live.
  • Just a random thought... Won't this be a tool to increase twitter spam?
  • It shouldn't be spam because we are only Tweeting "Published" articles, therefore the article has received enough votes and hasn't been flagged to be spam, so it should be safe to Tweet.

    I would like to come up with a way to cache it so that we only allow it to Tweet every 30 minutes max.
  • I added an application onto my Facebook which lets me add my Twitter status to my Facebook status. There are a few different kinds. I searched for twitter on my Facebook search bar and chose "Selective Twitter Status" because I don't want to put all my Twitter status' onto Facebook only some of them.
  • I also use that app, it's a fantastic way to promote posts on both services.
  • it's a fantastic way but I prefer to use friendfeed for this crossposting
  • Yes, you can send as a text. First go to your Twitter account and go to "devices" under "settings" and type in your number and save. They will give you a code that you have to send as a text to the number they provide depending where you live:
  • Do you share a very useful article. learned thanks to you
  • I'll try auto publish my blog on twitter. Thanks for information.
blog comments powered by Disqus