Friday, May 16th, 2008

Let PHP Randomly Update Your Twitter Feed

So you’ve got a Twitter account…and you find yourself in one of the following situations:

  1. You’re going on vacation, but don’t want to have people thinking you’re MIA.
  2. You’re sick and tired of updating your twitter account and you don’t want people “nudging” you.
  3. You have an NMS — a “new media stalker.”  So you’ve created a secondary twitter account for them to follow but don’t want to actually update it yourself.

//
//Twitter updater, v1.0
//by Jon McCartie
//
//functions first
function twitter_post($message) {

$username = ‘yourtwitterusername’;
$password = ‘yourtwitterpassword’;

// The twitter API address
$url = ‘http://twitter.com/statuses/update.xml’;
// Alternative JSON version
// $url = ‘http://twitter.com/statuses/update.json’;
// Set up and execute the curl process
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, “$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=$message”);
curl_setopt($curl_handle, CURLOPT_USERPWD, “$username:$password”);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);

// check for success or failure
if (empty($buffer)) {
$msg = “ERROR connecting to Twitter”;
return $msg;
} else {
$msg = “SUCCESS!! TWEET SENT!”;
return $msg;
}
}

//begin loop. this loop is needed to make sure the quote we get is less than 140 characters
$i=0;
while ($i == 0) {

//get random quotes XML — we’re using quotedb.com because they return quotes in XML
$url = “http://www.quotedb.com/quote/quote.php?action=random_quote_rss&=&=&”;

//this CURL script allows for the URL to be passed without exposing PHP to a allow_url security bug
$ch = curl_init($url);
$fp = fopen(”temp2.xml”, “w”);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

//Load the local XML that was created in the above CURL call into an array
$xml = simplexml_load_file(”temp2.xml”)
or Die (’ERROR: Could not load file’);

//read the first item’s quote, and strip the quotes
$message = trim($xml->channel->item->description,”\x22\x27″);

//if the message is less than 140 characters,
if (strlen($message) <= 140) {

//fire the Tweet
$msg = twitter_post($message);
echo “Success!”;
$i = 1; // set $i to 1 so we can end the loop
} //end if
} // end while loop
?>

Now you have a couple of choices. Place this on your web account and run a cron every few hours. Or, run it locally. Throw it in your bookmarks list and call the PHP page a few times a day. It all depends on how dedicated you are to your little ploy. Enjoy!

Tags: > > > >

Thursday, May 15th, 2008

Twitter Class for PHP

David Billingham has put together an awesome PHP class full of Twitter functions:

He provides a few examples of usage in the source file.  Here’s an example of using the class to post a direct message with a little extra error output:

$message = “your direct message”;
$t= new twitter();
$t->username= “yourtwitterusername”;
$t->password= “yourtwitterpassword”;
$res = $t->sendDirectMessage(”directmsgrecepient,$message);
if($res===false){ //if there is a Twitter error
echo ‘<span class=”style7″>Something went wrong!</span> <br/>Twitter said: <span class=”style5″>’ . $t->errmsg . ‘</span><br/>  Please try again.’;
}else{
echo ‘<span class=”style3″>SUCCESS!!  TWEET SENT!</span><br/>’;

}

Tags: > > > >

Wednesday, May 14th, 2008

Dreamhost + Wordpress + Posting Code

Use dreamhost?  Use wordpress?  Want to post PHP code inside a “<code>” tag and it’s not working?

  1. Visit your web panel (panel.dreamhost.com)
  2. Go to the manage domains panel (https://panel.dreamhost.com/index.cgi?tree=domain.manage&)
  3. Edit the domain you’re using
  4. Turn OFF “Extra Web Security?”

    Wow, that took me a long time to figure out.

    Tags: > > > > >

    Tuesday, May 13th, 2008

    PHP: Importing Remote XML To an Array

    XML. Your friend, your enemy. Perhaps it’s overused, but do enough work with a few 3rd party systems and you’d better have a solution for throwing XML into an array.

    There is a potential misuse of fopen() to call external XML’s and many hosted service providers will forbid you from using it to call external files. Therefore, I humbly suggest using CURL to do your dirty work. It’s a few extra lines of code, but it’s worth it.

    //the location of your external XML file. Basic HTTP Auth can be added if necessary
    $url = “http://someurl.com/somefile.xml”;

    //grab the XML and save it to a temp XML file
    $ch = curl_init($url);
    $fp = fopen(”temp.xml”, “w”);
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_exec($ch);
    curl_close($ch);
    fclose($fp);

    //Load the local XML that was created in the above CURL call
    $xml = simplexml_load_file(”temp.xml”)
    or Die (’ERROR: Could not load file’);

    print_r($xml);

    Tags: > > >