XML Data PHP Example2

YWAM Staff Opportunities XML Feed Using PHP - Example 2
This example from the ywamharpenden.org web site uses a newer (safer) curl library to load the xml data file from ywam.org. The reason why we are using curl here is that off side loading of xml is now turned off for security reasons.

The example is also from a Wordpress site and the xsl transform file is being loaded locally from the Wordpress theme template folder.

Ensure that XSLT processing and CURL are enabled on your server. Most modern server packages with php installed will have XSLT processing and CURL already enabled. The way to check this is to run a phpinfo script and check that XSLT support is enabled by scrolling down until you find the line:

curl curl support enabled

xslt XSLT support enabled

<?php //phpinfo; //print ""; $ch = curl_init;    // Init curl $timeout = 0;         // set a time out value $url="http://example.ywam.org/xmldata/staffopps.xml?bid=56";      // define url to load data from (note this is not the correct url to use please contact Colin Forbes for the correct info) curl_setopt ($ch, CURLOPT_URL, $url);                            // Tell curl to load data from this url curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);                    // Set a few curl options curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $rawdata = curl_exec($ch);                                       // Get the xml data and put it in $rawdata curl_close($ch);                                                 // remember to close components that you use to free resources

//print $rawdata;     // uncomment this line and check your html browser source to see if the xml file has been loaded or not.

//print ""; $xml = new SimpleXMLElement($rawdata);                         // Using SimpleXML to create a new xml document using the raw xml //print $xml->asXML; // uncomment this line and check your html browser source to see if the xml file has been loaded or not.

//print ""; $xsl = new DOMDocument;                                           // Create a new XML document for the XSLT transform file $xsl->load(get_bloginfo('template_directory') . '/staffopps.xsl'); // Load your XSLT transform file (locally from your server) //print 'XSLT Location:'. get_bloginfo('template_directory'). '/staffopps.xsl'; // get_bloginfo is a wordpress call - uncomment this to see where the XSLT file is being loaded from and check that it exists //print $xsl->saveXML; // uncomment this line and check your html browser source to see if the XSLT file has been loaded or not.

//print "";

$proc = new XSLTProcessor;       // Create an XSLTProcessor for the transform $proc->importStyleSheet($xsl);   // Load the XSLT style sheet

echo $proc->transformToXML($xml); // echo the result of the transform to the browser ?>

Here is the XSLT style template we have used.      