Development

Use PHP to display Gravatar images


All URLs on Gravatar are based on the use of the hashed value of an email address. According to their best practice you should:

  1. Trim leading and trailing whitespace from an email address
  2. Force all characters to lower-case
  3. md5 hash the final string

Of course in PHP this is trivial as the following function shows:

function fetchGravatar() {
$fetchHash = md5(strtolower(trim('your_email.address.com')));
$GravatarImage = 'http://www.gravatar.com/avatar/' . $fetchHash;
return $GravatarImage;
}

Profiles are also available if you require them.

Use PHP to display Gravatar images Read More »

CSV to .plist conversion

Property list files (.pfiles) store serialized objects and are often used to store a user’s settings. They are also used to store information about bundles and applications in OSX and recently iOS.

Danilo Campos wrote some time ago some background information and provided his source and blogged about the subject. Better than that however, he provided an OSX binary to download if you want to get going straight away.

This little application makes it easy to format and process your source data in a familiar environment like Excel and then convert ready to use in your application.

EDIT: It seems Danilo has taken his blog offline, and also this binary. I suggest you now use this online tool: https://wtools.io/convert-csv-to-plist

CSV to .plist conversion Read More »

PHP Host Settings Without phpinfo()


Occasionally a webhost will block the very useful phpinfo() command making it difficult to understand any limitations of the server configuration. This script can display relevant information to help you understand what is happening behind the scenes.

<?php
$d_func = ini_get("disable_functions");
$up_max = ini_get("upload_max_filesize");
$up_max = str_replace("M","",$up_max);
$up_max_size = "Megabytes";
$post_max = ini_get("post_max_size");
$post_max = str_replace("M","",$post_max);
$post_max_size = "Megabytes";
$input_max = ini_get("max_input_time");
$mem_limit = ini_get("memory_limit");
$mem_limit = str_replace("M","",$mem_limit);
$mem_limit_size = "Megabytes";
$exec_time = ini_get("max_execution_time");
$safe_mode = ini_get("safe_mode");

if(!is_null($d_func) && $d_func !== ""){echo "Disabled Functions: \n $d_func";}
if(!is_null($safe_mode) && $safe_mode !== ""){echo "<span style='color:red;'>Safe Mode is Active</span> <br>";}
if($up_max >= 1001){$up_max = $up_max / 1024; $up_max_size = "Gigabytes";
if($up_max >= 10001){$up_max = $up_max / 1024; $up_max_size = "Terabytes";}}
if($post_max >= 1001){$post_max = $post_max / 1024; $post_max_size = "Gigabytes";
if($post_max >= 10001){$post_max = $post_max / 1024;$post_max_size = "Terabytes";}}
if (min($input_max,60)){$input_max = $input_max /60;}
if (min($exec_time,60)){$exec_time = $exec_time /60;}
if($mem_limit >= 1001){$mem_limit = $mem_limit / 1024; $mem_limit_size = "Gigabytes";}

echo "Maximum Upload Size = $up_max $up_max_size<br>";
echo "Maximum Post Size = $post_max $post_max_size <br>";
echo "Maximum Input Time = $input_max minute/s<br>";
echo "Memory Limit = $mem_limit $mem_limit_size<br>";
echo "Maximum Execution Time = $exec_time minute/s<br>";
?>

PHP Host Settings Without phpinfo() Read More »

PHP Modulo Operator For Alternating Rows


Alternating row colours within a grid of data can make for a more pleasant viewing experience and has become a popular styling method in recent times. I was a little incredulous at some code I saw recently to determine alternating rows so without further introduction, meet the Modulo operator which is a quick and easy way to get up and running:

for($i=0;$i<10;$i++){ if($i % 2){ // This is an even row }else{ // This is an odd row } }

I'm sure you will be able to apply this to your own css or styling within your loops.

PHP Modulo Operator For Alternating Rows Read More »

Use cURL to obtain remote filesize


Sometimes you might want to determine a remote file size prior to determining further action to take. This is a quick function that utilises cURL to retrieve the file size in bytes.
<?php
function remoteFilesize ($URL){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$info = curl_exec($ch);
curl_close($ch);
$info = explode('Content-Length:', $info);
$info = explode("Connection",$info[1]);
return $info[0];
}
?>

Usage is simple:

echo remoteFilesize("test.com/file.html");

Use cURL to obtain remote filesize Read More »

Get Facebook Likes and Shares via Graph API


Facebook Like and Share count can easily be obtained from the Facebook Graph API. This is an extremely quick way to look up the JSON response from Facebook so that you can further process it for your own requirements.
<?php
$URL ='https://graph.facebook.com/cocacola';
$JSON = file_get_contents($URL);
$Output = json_decode($JSON);
$Likes = 0;
if($Output->likes){
  $Likes = $Output->likes;
}

echo "Number of likes for CocaCola's Facebook Page = ".$Likes."</br>";

$URL2 ='https://graph.facebook.com/http://www.coca-cola.com';
$JSON2 = file_get_contents($URL2);
$Output2 = json_decode($JSON2);
$Shares = 0;
if($Output2->shares){
  $Shares = $Output2->shares;
}

echo "Number of shares for coca-cola.com = ".$Shares;
?>

UPDATE 29/01/2012: I’ve written a slightly different method using the PHP SDK in this post here

Get Facebook Likes and Shares via Graph API Read More »

Currency Conversion using Yahoo Finance API


Just a quick and dirty PHP function to retrieve information from the Yahoo Finance API to perform a currency conversion
function ConvertCurrency($From,$To,$Amount){
$URL = 'http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s='. $From . $To .'=X';
$Handle = @fopen($URL, 'r');

if ($Handle) {
$Result = fgets($Handle, 4096);
fclose($Handle);
}
$allData = explode(',',$Result);
$Value = $allData[1];

return $Value * $Amount;
}

Usage:
$Amount = ConvertCurrency("NZD","USD",25);

Currency Conversion using Yahoo Finance API Read More »

Using mod_rewrite to remove variables from a URL


The Apache mod_rewrite module offers an unparalleled toolset for manipulating URLs, – however configuration can be a tricky beast at times. While most people are comfortable enough removing file extensions like .php we recently had a user who wanted to hide all the variables in the address string.

For example index.php?id=id would end up being index/id
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^about/(.*)$ index.php?id=$1 [QSA]

The magic here is the QSA or Query String Append. If you just wanted to remove the .php you would use
RewriteCond %{REQUEST_URI} !.*\.php$
RewriteRule ^/(.*)$ $1.php [QSA]

Using mod_rewrite to remove variables from a URL Read More »