Plugin to update twitter with blog posts?

rgordon83

it's a wig
Dec 27, 2007
1,310
28
0
www.tribe9interactive.com
I used to use twitter updater but the damn thing stopped working for me a year ago and i have NO idea why. Does anyone know of another plugin that does the same thing (Posts a tweet when i write a new blog post with a link to my blog)??
 


Yeah I use Twitterfeed, too, but there are still some accounts that have stopped updating for some unknown reason (with very active RSS feeds).
 
OK I read this place all the time and have got a lot out without giving anything back so here goes;

Put these two in the same folder :

whatever.php
PHP:
<?php
include('last.php');
$uname = 'TWITTER_USERNAME';
$pwd = 'TWITTER_PASSWORD';

$twitter_url = 'http://twitter.com/statuses/update.xml';
$feed = "RSS_FEED_GOES_HERE"; 
$rss = new lastRSS;
if ($rs = $rss->get($feed)){
    $title = $rs[items][0][title];
    $url = $rs[items][0][link];
} else { die('Error: RSS file not found.'); }
$bitly_call =  file_get_contents("http://api.bit.ly/shorten?version=2.0.1&longUrl=" . $url . "&login=BITLY_USER_NAME&apiKey=BITLY_API_KEY&history=1");
$bitlyinfo=json_decode(utf8_encode($bitly_call),true);
$tiny_url= $bitlyinfo['results'][$url]['shortUrl'];
$status = $title . " " . $tiny_url;
echo $status;
$curl_handle = curl_init();
curl_setopt($curl_handle,CURLOPT_URL,"$twitter_url");
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl_handle,CURLOPT_POST,1);
curl_setopt($curl_handle,CURLOPT_POSTFIELDS,"status=$status");
curl_setopt($curl_handle,CURLOPT_USERPWD,"$uname:$pwd");
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
if (empty($buffer)){echo '<br/>message';}else{echo '<br/>success';}?>
last.php :
PHP:
<?php
class lastRSS {

    var $default_cp = 'UTF-8';
    var $CDATA = 'content';
    var $cp = '';
    var $items_limit = 0;
    var $stripHTML = False;
    var $date_format = '';

    var $channeltags = array ('title', 'link', 'description', 'language', 'copyright', 'managingEditor', 'webMaster', 'lastBuildDate', 'rating', 'docs');
    var $itemtags = array('title', 'link', 'description', 'author', 'category', 'comments', 'enclosure', 'guid', 'pubDate', 'source');
    var $imagetags = array('title', 'url', 'link', 'width', 'height');
    var $textinputtags = array('title', 'description', 'name', 'link');

    function Get ($rss_url) {
        if ($this->cache_dir != '') {
            $cache_file = $this->cache_dir . '/rsscache_' . md5($rss_url);
            $timedif = @(time() - filemtime($cache_file));
            if ($timedif < $this->cache_time) {
                $result = unserialize(join('', file($cache_file)));
                if ($result) $result['cached'] = 1;
            } else {
                $result = $this->Parse($rss_url);
                $serialized = serialize($result);
                if ($f = @fopen($cache_file, 'w')) {
                    fwrite ($f, $serialized, strlen($serialized));
                    fclose($f);
                }
                if ($result) $result['cached'] = 0;
            }
        }
        else {
            $result = $this->Parse($rss_url);
            if ($result) $result['cached'] = 0;
        }
        return $result;
    }

    function my_preg_match ($pattern, $subject) {
        preg_match($pattern, $subject, $out);
        if(isset($out[1])) {
            if ($this->CDATA == 'content') { // Get CDATA content (without CDATA tag)
                $out[1] = strtr($out[1], array('<![CDATA['=>'', ']]>'=>''));
            } elseif ($this->CDATA == 'strip') { // Strip CDATA
                $out[1] = strtr($out[1], array('<![CDATA['=>'', ']]>'=>''));
            }
            if ($this->cp != '')
                //$out[1] = $this->MyConvertEncoding($this->rsscp, $this->cp, $out[1]);
                $out[1] = iconv($this->rsscp, $this->cp.'//TRANSLIT', $out[1]);
            return trim($out[1]);
        } else {
            return '';
        }
    }

    function unhtmlentities ($string) {
        // Get HTML entities table
        $trans_tbl = get_html_translation_table (HTML_ENTITIES, ENT_QUOTES);
        // Flip keys<==>values
        $trans_tbl = array_flip ($trans_tbl);
        // Add support for &apos; entity (missing in HTML_ENTITIES)
        $trans_tbl += array('&apos;' => "'");
        // Replace entities by values
        return strtr ($string, $trans_tbl);
    }

    function Parse ($rss_url) {
        if ($f = @fopen($rss_url, 'r')) {
            $rss_content = '';
            while (!feof($f)) {
                $rss_content .= fgets($f, 4096);
            }
            fclose($f);

            $result['encoding'] = $this->my_preg_match("'encoding=[\'\"](.*?)[\'\"]'si", $rss_content);
            if ($result['encoding'] != '')
                { $this->rsscp = $result['encoding']; } // This is used in my_preg_match()
            else
                { $this->rsscp = $this->default_cp; } // This is used in my_preg_match()

            preg_match("'<channel.*?>(.*?)</channel>'si", $rss_content, $out_channel);
            foreach($this->channeltags as $channeltag)
            {
                $temp = $this->my_preg_match("'<$channeltag.*?>(.*?)</$channeltag>'si", $out_channel[1]);
                if ($temp != '') $result[$channeltag] = $temp; // Set only if not empty
            }
            if ($this->date_format != '' && ($timestamp = strtotime($result['lastBuildDate'])) !==-1) {
                        $result['lastBuildDate'] = date($this->date_format, $timestamp);
            }
            preg_match("'<textinput(|[^>]*[^/])>(.*?)</textinput>'si", $rss_content, $out_textinfo);
            if (isset($out_textinfo[2])) {
                foreach($this->textinputtags as $textinputtag) {
                    $temp = $this->my_preg_match("'<$textinputtag.*?>(.*?)</$textinputtag>'si", $out_textinfo[2]);
                    if ($temp != '') $result['textinput_'.$textinputtag] = $temp; // Set only if not empty
                }
            }
            // Parse IMAGE info
            preg_match("'<image.*?>(.*?)</image>'si", $rss_content, $out_imageinfo);
            if (isset($out_imageinfo[1])) {
                foreach($this->imagetags as $imagetag) {
                    $temp = $this->my_preg_match("'<$imagetag.*?>(.*?)</$imagetag>'si", $out_imageinfo[1]);
                    if ($temp != '') $result['image_'.$imagetag] = $temp; // Set only if not empty
                }
            }
              preg_match_all("'<item(| .*?)>(.*?)</item>'si", $rss_content, $items);
            $rss_items = $items[2];
            $i = 0;
            $result['items'] = array(); // create array even if there are no items
            foreach($rss_items as $rss_item) {
                if ($i < $this->items_limit || $this->items_limit == 0) {
                    foreach($this->itemtags as $itemtag) {
                        $temp = $this->my_preg_match("'<$itemtag.*?>(.*?)</$itemtag>'si", $rss_item);
                        if ($temp != '') $result['items'][$i][$itemtag] = $temp; // Set only if not empty
                    }
                    // Strip HTML tags and other bullshit from DESCRIPTION
                    if ($this->stripHTML && $result['items'][$i]['description'])
                        $result['items'][$i]['description'] = strip_tags($this->unhtmlentities(strip_tags($result['items'][$i]['description'])));
                    // Strip HTML tags and other bullshit from TITLE
                    if ($this->stripHTML && $result['items'][$i]['title'])
                        $result['items'][$i]['title'] = strip_tags($this->unhtmlentities(strip_tags($result['items'][$i]['title'])));
                    if ($this->date_format != '' && ($timestamp = strtotime($result['items'][$i]['pubDate'])) !==-1) {
                        // convert pubDate to specified date format
                        $result['items'][$i]['pubDate'] = date($this->date_format, $timestamp);
                    }
                    $i++;
                }
            }

            $result['items_count'] = $i;
            return $result;
        }
        else 
        {
            return False;
        }
    }
}
?>
Stick the first one on a cron, or if you are a bit weird like me and like to inexplicably load the first script from a couple of other servers do it something like this and cron it :
PHP:
<?php
$url = "URL_OF_FIRST_SCRIPT.php";
$raw = file_get_contents($url);
$content = html_entity_decode($raw);
echo "$content";
?>
 
Last edited: