Friday, October 17, 2008

Figuring out PHP DOMDocument and XSLTProcessor #php

Although I calculate the period I've been programming in decades, I am a relative PHP newbie. I'm testing and playing with PHP and writing distributed services, yet it's sometimes the simple knowledge that I'm missing. (How does this Object perform the function that I want.) So I've been playing with the XML Classes in PHP and modified so code from PHP + Twitter + Google Calendar + SMS
. The article is quite interesting, and I discovered the Zend Framework, which makes my life much easier. :)

<?php
// Define credentials for the Twitter account
define('TWITTER_CREDENTIALS', 'USERNAME:PASSWORD');

// Set up CURL with the Twitter URL and some options
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://twitter.com/statuses/friends_timeline.rss');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// Twitter uses HTTP authentication, so tell CURL to send our Twitter account de
tails
curl_setopt($ch, CURLOPT_USERPWD, TWITTER_CREDENTIALS);


// Execute the curl process and then close
$data = curl_exec($ch);
curl_close($ch);


// MY CHANGES START HERE:

// Load XML
$dom = new DOMDocument();
$dom->loadXML(print_r($data, true));

// Load XSL:
$filename = "rss.xsl"; // You can find an rss.xsl file lots of places.
$domxsl = new DOMDocument();
$domxsl->load($filename);
$xsltp = new XSLTProcessor();
$xsltp->importStylesheet($domxsl);

// Apply XSL to XML
$result = $xsltp->transformToXML($dom);

// Output XSL Transformed XML
print ($result);
?>

Technorati technorati tags: , , , ,

Labels: