How To: PHP OAuth Twitter

So, you’re building a web based twitter client in PHP.  Easy, just use a simple Twitter API client and call the Twitter API directly.  But what if you don’t want to store the user’s Twitter authentication information anywhere?  It’s a security risk anyway and some people don’t want to give up their account details to a 3rd party.  So what to do?  The solution is to use OAuth with Twitter. Continue reading “How To: PHP OAuth Twitter”

How-To: Removing trailing whitespace from a string with php

Need to remove the trailing whitespace characters from a string in php? chomp() is how you do it in Perl, however in PHP it’s just as easy. Simply use rtrim():

// $string = "|my string with trailing whitespace  |"
$string = rtrim($string);

// $string = "|my string with trailing whitespace|"

It’s that easy! There is also the trim() function which does the same at the start of the line. You can view the manual reference here.

How-To: Read an RSS Feed with PHP

An easy way to read an RSS feed with PHP is to use the MagpieRSS library. This provides a simple API to read an RSS feed, and elegantly deal with the issues of timeouts and caching.

It’s as simple as:

require('rss_fetch.inc');
$rss = fetch_rss($url);
echo "Site: ", $rss->channel['title'], "<br>
";
foreach ($rss->items as $item ) {
	$title = $item[title];
	$url   = $item[link];
	echo "<a href=$url>$title</a></li><br>
";
}

More information is available from the MagpieRSS website

How-To: Determine Real hostname in php

If you need to know the real host name of your server using php, use the following command:

<? echo gethostbyaddr (gethostbyname ($_SERVER["SERVER_NAME"])); ?>

This will return the server’s hostname, not the address that the script is being accessed by. Very useful if you have a script that needs to run on multiple servers.

How-To: Fix SimpleXML CDATA problem in php

If you’ve used the SimpleXML functions in PHP, you may have noticed some strange things happening with CDATA values in your XML file/string. All I needed to do was extract the value of my CDATA fields, however these were always coming back blank in the structure that simplexml_load_file returns.

Finally, after hours of trawling google, I’ve come up with the following solution:

$xml = simplexml_load_file($this->filename,
'SimpleXMLElement', LIBXML_NOCDATA);

Use this line of code when you are loading the XML file into the SimpleXML Object.  The key is the LIBXML_NOCDATA option as the third parameter. This returns the XML object with all the CDATA data converted into strings. You can read about this in the php manual.

This solved all the problems I was having getting CDATA values out of SimpleXML in php. Hope it helps someone.

How-To: Parse XML with PHP (SimpleXML)

I’ve been looking for a simple way to convert XML into an array in PHP, like the Perl library XML::Simple does.  The best bet with PHP is SimpleXML, which provides a pretty simple interface for parsing XML in PHP.

Take a simple example XML file:


<list type='randomlist'>
   <item>
       <name>Item Number 1</name>
       <code>ITEM1</code>
   </item>
</list>

To parse this with SimpleXML, do the following:

// If we have the XML in a string we do this: 
$xml = new SimpleXMLElement($xmlstr);

// If we have the XML in a file we do this:
$xml = simplexml_load_file($filename);

...

// We can now access the XML values and attributes easily
$name = $xml->list[0]->item->name;
echo $name;   // "Item Number 1"

// Attributes too (these are NOT documented on the PHP site!)
$list_version = $xml->list['type'];
echo $list_verson;  // "random list"

More information is available from the PHP website, and there is a really useful blog here too.

PHP Date in Human Readable Form (Facebook Style)

I like to display dates relative to now, aka Facebook.  So instead of displaying a date in the form 3-2-2008 (which can get confusing if you are in the US/Europe etc with dates being switched) I like to show it as 2 weeks ago as this is far easier to read.

So I came accross the following function which produces these dates in PHP:

function RelativeTime($timestamp){
$difference = time() - $timestamp;
$periods = array("sec", "min", "hour", "day", "week",
"month", "years", "decade");
$lengths = array("60","60","24","7","4.35","12","10");

if ($difference > 0) { // this was in the past
$ending = "ago";
} else { // this was in the future
$difference = -$difference;
$ending = "to go";
}
for($j = 0; $difference >= $lengths[$j]; $j++)
$difference /= $lengths[$j];
$difference = round($difference);
if($difference != 1) $periods[$j].= "s";
$text = "$difference $periods[$j] $ending";
return $text;
}

Original post is here.

Upgrading PHP on Fedora Core 5 (or other distro)

Download the RPMs you require for your new PHP installation from the following URLs. If you have RPMs that aren’t listed, use the search function:

Or if you are upgrading another distrobution, search for the equivalents here.

Check which PHP RPMs you currently have installed:

rpm -qa | grep phpphp-5.1.6-1.6

Output should be something like:

php-pear-1.4.9-1.2

php-pdo-5.1.6-1.6

php-gd-5.1.6-1.6

php-mysql-5.1.6-1.6

** Do The Following Steps As ‘Root’ **

Now we need to remove the installed PHP RPMs prior to installing our new RPMs:

rpm -e php php-pear php-pdo php-gd php-mysql

Now install the PHP modules as required:

rpm -Uvh pcre-6.6-1.fc5.remi.i386.rpm
rpm -ivh php-common-5.2.5-1.fc5.remi.i386.rpm
rpm -ivh php-cli-5.2.5-1.fc5.remi.i386.rpm
rpm -ivh php-5.2.5-1.fc5.remi.i386.rpm
rpm -ivh sqlite2-2.8.17-1.fc5.i386.rpm
rpm -ivh php-pdo-5.2.5-1.fc5.remi.i386.rpm
rpm -ivh php-mysql-5.2.5-1.fc5.remi.i386.rpm
rpm -ivh php-pear-1.7.1-1.fc5.remi.noarch.rpm

Now restart apache:

apachectl graceful

And we should have php version 5.2.5 installed!