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!

  • Your Tweets will automatically be added to edocr.com's own channel on Twitter, once you configured your Twitter settings. Click here to access our Twitter channel. You could also search what others are tweeting about edocr by searching. Click here to access search results
  • that solves my problem too...
  • Thanks a lot! That solves my problem. The "system" has posted all my posts ... Unfortunately, he did so "backwards", I mean, the last tweet is the oldest post in my blog.
  • Great Information and surely i will use it
  • SEO
    Social media in recent years has gained importance.For social media to create something very beautiful.Thank you for sharing.
  • can you write this for phpld directories.
  • 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.
blog comments powered by Disqus