My First Attempt With Phing

I've finally come to the place where I need some help in automated deployments. I've got a few weekend PHP projects that are getting large enough that deploying them takes a few minutes -- minutes where stuff can break and stay broken until I remember the exact command line call I need to make to fix them. SVN export, symlinks, mkdir's, etc etc. I want to automate this crap!

My first attempt was with using Capistrano. I'd heard it could be hacked a bit to make it deploy just about anything. So I installed the gem and tinkered. The trouble is, I don't have time to learn Ruby right now. Furthermore, I don't like having the Ruby/Capistrano dependencies.

...and in walks Phing -- automated deployment using PHP and XML. Hey! I know those!

Installing Phing was a bit more troublesome than Capistrano -- mainly because my hosting company (Dreamhost) has some restrictions around installing PEAR modules and also a small issue in that the default command line bin for PHP is v4 -- no good for Phing, which requires PHP5.

A few hours later, and viola! Phing is ready to roll.

Loading mentions Retweet
Filed under  //  capistrano   deployment   dreamhost   phing   php   tech  
Comments (0)
Posted 5 months ago

PHP's "strtotime" vs JAVA's ... nothing

PHP's "strtotime" function is one of the coolest core functions of the language.  I never really noticed how great it was until today.

At my company, we import date formats from customers and need to store them in MySQL format. ('Y-m-d h:m:s').  In PHP, you can pass strtotime just about anything and -- with the help of the date function -- convert that wiley timestamp to what you want.

Consider the following time formats:


  • 2004-02-12T15:19:21+00:00 (ISO 8601)

  • Thu, 21 Dec 2000 16:01:07 +0200 (RFC 2822)

  • Monday, January 1st

  • tomorrow

  • -1 week 2 days 4 hours 2 seconds


Let's convert these to MySQL timestamps with PHP:
echo date('Y-m-d h:m:s', strtotime('2004-02-12T15:19:21+00:00'));
echo date('Y-m-d h:m:s', strtotime('Thu, 21 Dec 2000 16:01:07 +0200'));
echo date('Y-m-d h:m:s', strtotime('Monday, January 1st'));
echo date('Y-m-d h:m:s', strtotime('tomorrow'));
echo date('Y-m-d h:m:s', strtotime('-1 week 2 days 4 hours 2 seconds'));

This automagically outputs:
2004-02-12 07:02:21
2000-12-21 06:12:07
2009-01-01 12:01:00
2009-02-12 12:02:00
2009-02-06 09:02:41

I remember the first time I can across strtotime and thought "wow - it will take in just about any format."  Clearly, there are some limits (I can't enter "orange" and get a date back) -- but needless to say, the function is simple and powerful.

Back to today at work.

A developer (we've recently moved to being an all-Java shop) tells me he has to write a custom date parser for ingesting and converting partner's date formats.  And every time we see a new date format, he has to add to his parser (then we have to build it, QA it, release it).

Poppy cock, I say!  Isn't there something in Java like strtotime?  Turns out?  No.

I did my googling, nothing.  Then I turned to Twitter.  Watch as we go from "But you *can* do that in Java!" to "oh, well, you'd have to be expecting a certain date format" quicker than you can say "ISO 8601."


  • Java Friend #1: "you can do that simply: String time = "12:31:24"; DateFormat sdf = new SimpleDateFormat("hh:mm:ss"); String result = sdf.parse(time).toString();"

  • Java Friend #2: "Won't SimpleDateFormat.parse do what you need?"

  • Me, to both: "Thanks for the suggestions. All the sample code I see is massive compared to the one-liner "strtotime." Sigh. Ah well.""

  • Java Friend #1 replies : "a one liner, but not as short as strtotime: System.out.println(new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z").parse(t));""

  • I direct message #1: "Will SimpleDateFormat accept multiple input date formats: ISO 8601, RFC 2822, MySQL Timestamp/Datestamp, etc.?"

  • His reply:  "you need to know the date format coming into SimpleDateTime. What format is it?"

  • Me: "There's the rub. Could be ISO 8601, RFC 2822, MySQL Timestamp/Datestamp, etc. Was hoping to not have to write for each possible..."

  • Him: "you'll need to do something about the type of date strings you operating on to make an error free conversion"


<slaps forehead>

And there's the problem -- I don't want to have to build in a switch for every known date format.  If partner XYZ wants to enter a non-standard format ("-1 week 2 days 4 hours 2 seconds"?), we shouldn't need to refactor the code to parse this.

Now.  A disclaimer.  I am not a JAVA programmer.  I consider myself an intermediate PHP programmer.  Programming is not my job nor is it my dream to become a full-time developer.

However, I've done my google...and I've asked 4 different JAVA experts who all agree it can't be done as cleanly as strototime.

That being said?  Booooo to you Java.  Boooooo.

Loading mentions Retweet
Filed under  //  date   dateformat   java   music   php   ramblings   simpledateformat   strtotime   tech  
Comments (0)
Posted 1 year ago

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!

Loading mentions Retweet
Filed under  //  php   ramblings   random   twitter   update   xml  
Comments (0)
Posted 1 year ago

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/>';

}

Loading mentions Retweet
Filed under  //  class   direct   message   php   tech   twitter  
Comments (0)
Posted 1 year ago

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.

    Loading mentions Retweet
    Filed under  //  code   dreamhost   hosting   php   pre   tag   tech  
    Comments (0)
    Posted 1 year ago

    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);

    Loading mentions Retweet
    Filed under  //  curl   fopen   php   tech   xml  
    Comments (0)
    Posted 1 year ago