XML Data PHP Example2

From YWAMKnowledgeBase
Jump to: navigation, search

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

//print "<!-------------------------------------------------------------------------- Getting XML Document -------------------------------------------------------------------->";
$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 "<!------------------------------------------------------------------------ Generating XML Document -------------------------------------------------------------------->";
   $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 "<!------------------------------------------------------------------------ Load XSLT Document -------------------------------------------------------------------->";
$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 "<!---------------------------------------------------------------------- Transforming  -------------------------------------------------------------------->";

$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.

<?xml version="1.0" encoding="ISO-8859-1"?>
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
     <xsl:output doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" encoding="iso-8859-1" method="html"/>
     <xsl:template match="/">
         <table width="100%" border="0" cellpadding="2" cellspacing="0">
             <tr><th width="25%" class="name">Name</th><th width="60%" class="description">Description</th><th width="15%" class="commitment" align="center">Commitment</th></tr>
             <xsl:for-each select="ROOT/OpLoc/ServiceOpportunity">
                     <td><xsl:value-of select="@OpportunityName"/></td>
                     <td><xsl:value-of select="." disable-output-escaping="yes"/></td>
                     <td align="center"><xsl:value-of select="@Commitment"/></td>