Last week at ZendCon08 I took my ZCE test for PHP5 and passed. I am now a ZCE!
Tag Archives: PHP5
Putting Twitter on Your Blog
Today I wanted to put my Twitter feed on my blog but didn’t really want to use a widget since that is for distribution purposes and I was looking for something more permanent. Twitter gives everyone the ability to see their tweets via rss or atom feeds so I choose to use the rss feed. Mine feed was located at http://twitter.com/statuses/user_timeline/2376811.rss. If you have PHP5 then parsing out an XML (rss) file is very easy thanks to the SimpleXML functions. One small problem that I ran into was that my host doesn’t allow me to read in the file like I wanted to. I had to use CURL to read the file save it locally and then reopen that file to parse it with SimpleXML. Let step though the code. First you have to get the data from the rss feed and use curl to store this as a new file on your local server.
//open the remote fee from twitter.
$ch = curl_init("http://twitter.com/statuses/user_timeline/2376811.rss");
//overwrite this feed to a local file
$fp = fopen ("twitter_feed.xml", "w");
curl_setopt ($ch, CURLOPT_FILE, $fp);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_exec ($ch);
curl_close ($ch);
fclose ($fp);
Then you have to parse the local file that you just created. Twitter’s RSS feed puts the feed data in item nodes but nested in a channel node. Here we will use SimpleXML (PHP5 Only!) and a foreach loop to parse through each item node.
$xml = simplexml_load_file ("twitter_feed.xml");
foreach ($xml->channel->item as $item) {
echo str_replace ("farrelley:", "", $item->description);
}
Basically that is the heart of the code. Next we will put it all together into a function. We will also add in a counter that when you call the function you can specify how many item nodes or tweets you want to see.
// Display Twitter Feed
function getTwitterFeed($disp) {
//use Curl to get the feed and store it locally
$ch = curl_init ("http://twitter.com/statuses/user_timeline/2376811.rss");
//open the local feed.
$fp = fopen ("twitter_feed.xml", "w");
//set the counter = 1 its the first time through the script/
$count = 1;
curl_setopt ($ch, CURLOPT_FILE, $fp);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_exec ($ch);
curl_close ($ch);
fclose ($fp);
//parse through locally stored feed
$xml = simplexml_load_file ("twitter_feed.xml");
foreach ($xml->channel->item as $item) {
//strip out my username each description has username: at beginning.
echo str_replace ("farrelley:", "", $item->description);
//if the count = the number to display then break out of foreach
if ($count == $disp) break;
//else increment count and continue.
else $count++;
}
}
Then you just have to add this call to the function where you want the data to be output.
getTwitterFeed (1);
You can see this in action (on the right sidebar) on my other blog at Living in the District (http://www.livinginthedistrict.com/). Like always and comments please post comments.
Indexing PDF’s – The Why?
After I published my small article on “Indexing PDF Documents with Zend_Search_Lucene” I was surprised to find it on the Zend Developer Zone blog. I had no idea that this would get the attention that it did and I thank everyone for checking it out. So now that you know how you would index a PDF, you may be asking why the heck would you do this?
Many companies large and small have support centers, either be in internal help desks or external help desks. In addition to the help desk, many companies publish PDF documents such as manuals, specs, services guides, and setup/connections guides etc. So instead of a help desk employee (or anyone) remembering what manual does what and what page everything is on, you can simple index these PDF files for easy searching. Just think about it this way, say you have 50 products all having 5 manuals each, that’s 250 manuals that you have to keep track of (not including how many pages each manual has). The easy way would be to index the PDF’s, add the necessary metadata to the manual, build a search form around a web page and wa-la. You have a easy way to search PDF files finding information quickly for a customer or whoever, and saving loads of time searching page by page for the same information.
Many companies do this, and many companies bloat how they are the best at doing it. So next time you are looking for a searchable PDF solution, remember that anyone can do this and it’s easy to do yourself.
Indexing PDF Documents with Zend_Search_Lucene
Part I
I along with many others have been trying and asking how to index and search PDF files. Once Zend released its Framework, which is a port of Java Lucene to PHP, I decided to jump on board and find a way to index and search PDF files. So… Lets get started.
The Setup…
We will use XPDF to parse the PDF files and Zend_Search_Lucene for indexing and searching. First we need to read the PDF files and get relevant PDF information such as Title, Author, Modification Date, PDF size, and number of pages in the file. To do all this, download XPDF and copy the pdftotext and pdfinfo into a directory on your web server. The pdfinfo call will get us metadata of the PDF and the pdftotext will convert the PDF to a text file. Converting the PDF to a text file will allow us to store the data in the index.
Next we will have to download the Zend Framework and copy it over to the web server. You can just copy over the Zend_Search folder and the necessary functions but for now we will make it easy and copy everything to the server.
The Code…
Lets start by parsing a file and storing the data into the index file. First we need to make sure that our include path is pointed to the working directory and that we can access the Zend Search Lucene functions.
/** Zend_Search_Lucene */
ini_set('include_path','/path/to/working/directory/);
require_once 'Zend/Search/Lucene.php';
SilverStripe Fist Glance
Read/Write has posted a good article about SilverStripe, a new lightweight CMS built on PHP5 and Ajax. I checked out the demo that they had up and it really looks pretty good. I haven’t done an install or looked at the code to change layout, but from what I can see, it looks fairly easily.
They have a couple of good starts on modules too. Some of these include
- E-commerce
- Forum
- Flickr
- Gallery
- Google Maps
- Blog
By far not as extensive as WordPress or Drupal but it’s a good start. I’m going to keep an eye on this New Zealand Company as they might make a bigger splash into the CMS world here soon. I would say that this CMS Package is a mix between a blogging CMS package and an Enterprise CMS package. Look for more to come on this topic.