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.






