PhP Stuff

Update Twitter using PHP

Posted in PhP Stuff on March 5th, 2009 by sway – 2 Comments

So, we have recently started posting our blog headers to Twitter so that people can follow the threads more easily. To do so, instead of constantly updating our content in two places, we modified our wordpress blog to post directly to Twitter when we publish new articles to the tech blog.

I would say that it took about 6 minutes to integrate the code and test the new addition. You should be able to do the same in no time!

This code helps you to do 1) build a tinyURL so that any links you require are short; and 2) post content to your Twitter status updates using the Twitter API.


function add_to_twitter($link, $status){
$tinyURL = $this->get_tiny_url($link);
// Set username and password
$username = 'username';
$password = 'password';
// The message you want to send
$status = $status . " " . $tinyURL;
// 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=$status");
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)){
echo "bad post";
}else{
echo "success";
}
}
//gets the data from a URL
function get_tiny_url($url){
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,'http://tinyurl.com/api-create.php?url='.$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}

Share/Save/Bookmark

Sharing PHP Code among domains on a shared hosting account with GoDaddy

Posted in PhP Stuff, Tech Tips on February 18th, 2009 by sway – Be the first to comment

Contrary to what the GoDaddy Technical Support team tells everyone… you CAN (yes, you can) share code between multiple domains on a shared hosting account with GoDaddy!!!

Basically, I am sharing with you my work around for fact that we’re not allowed to create symlinks in the sub folders (which hold the content of the other domains (secondary, non-primary domains)) of our unlimited shared hosting account.

You can complete this work around in 3 easy steps!

  1. In the root directory of your unlimited hosting account, create a php5.ini file which is a copy of the already given php.ini file
  2. Add this line to your php5.ini file:
    include_path = ".:/usr/local/php5/lib/php:/home/content/s/a/m/sample/html/shared_code"
  3. Place some code in your shared_code directory and you are done!

You will be able to access the classes and any scripts you have in that directory by including the file with no path. This means your includes will look like this:
include ("SharedClass.php");

(You can also change your fopen status in your php.ini file, but you can use cURL to work around any other fopen issues holding you up)

NOTE: This solution assumes that you are running a Linux server (not Windows) and are using PHP 5 (although this solution may work for PHP 4 on Linux if you make the changes to the php.ini file and not the php5.ini file)

Share/Save/Bookmark

PhPDocs - Documentation App for PHP Code

Posted in PhP Stuff on February 18th, 2009 by sway – Be the first to comment

We have started to use PHPDocumentor to create JavaDocs-eqsue documentation of our PHP code.

The application itself is very easy to modify in the event that you want to alter the stylesheets to make the documents be more cohesive with your site, or if you are colorblind and want to get away from the tragic red-on-blue color scheme that is the current default.

http://www.phpdoc.org/

Some tips:

When documenting parameters using the @param tag format your documents as follows:
@param type $name description to follow
@param type $name2 description to follow

When documenting return type, you can either leave the return type blank (exclude the tag) and you will have a return type of void, or you can document your non-void return type as follows:

@return string description to follow

in my examples, “description to follow” is where I go into detail about the significance of the parameter or return object.

Also, be sure to format your comments like so:

/**
*     @param type $name
*     @param type $name2
*     @return type
*/

Share/Save/Bookmark