Location: PHPKode > scripts > XML Currency Reader > xml-currency-reader/XML_currency_reader.class.php
<?php
/**
 * XML CURRENCY READER CLASS
 * 
 * this class is intended to provide general support to read the xml with currencies
 * from a national bank site
 *
 * @author Cristian Năvălici {@link http://www.lemonsoftware.eu} lemonsoftware [at] gmail [.] com
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 * @version 1.0 26 Oct 2008
 * 
 */

abstract class XML_currency_reader {

    /**
    * the extended classes must implement these methods
    */
    abstract protected function parse_rate_contents($content, $currency = 'EUR');
    abstract protected function parse_domdoc();

    /**
    * xml file on the BNR site
    */
    protected $xmlpath;

    /**
    * if not enable we use curl functions; otherwise file_get_contents
    */
    protected $allow_url_fopen;

    /**
    * saves the xml content from get_xml_content()
    */
    protected $xml_content;

    /**
    * saves the DOMDocument from DOMconnector()
    */
    protected $domdoc;


    // ----------------------------------------------------------------------------
    /**
    * CONSTRUCTOR
    *
    * 
    * @param none
    * @return void
    */
    public function __construct() {
        $this->allow_url_fopen = ini_get('allow_url_fopen');
    }


    // ----------------------------------------------------------------------------
    /**
    * PARSE XML
    *
    * this calls other methods in order to get the content, make a DOMDocument from it
    * and parse it
    * 
    * @param none
    * @return array of arrays (parse_domdoc return value) | FALSE
    */
    public function parse_xml() {
        // gets the xml content
        $this->get_xml_content();

        // check if the connection was available
        if ( $this->xml_content ) {
            // creates a DOMDocument
            $this->DOMconnector();

            // parse the DOMDocument
            return $this->parse_domdoc();
        } else return FALSE;
    }
    // ----------------------------------------------------------------------------
    /**
    * GET THE XML CONTENT
    *
    * retrieve the remote file content using any of the methods available
    * saves it into a class var
    * 
    * @param none
    * @return void
    */
    protected function get_xml_content() {
        try {
            if ( $this->allow_url_fopen ) {
                // file_get_contents approach
                $xml_content = file_get_contents($this->xmlpath); 
            } else {
                // CURL approach
                // make sure curl is installed
                if ( function_exists('curl_init') ) {
                    // initialize a new curl resource
                    $ch = curl_init();

                    // set the url to fetch
                    curl_setopt($ch, CURLOPT_URL, $this->xmlpath);

                    // don't give me the headers just the content
                    curl_setopt($ch, CURLOPT_HEADER, 0);

                    // return the value instead of printing the response to browser
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

                    $xml_content = curl_exec($ch);

                    // remember to always close the session and free all resources
                    curl_close($ch);
                } else {
                    throw new Exception('Curl library is not installed; modify allow_url_fopen to 1 in php.ini');
                }
            }

            $this->xml_content = $xml_content;
        } catch (Exception $e) {
            echo 'BNR_reader->get_xml_content: ' .$e->getMessage(); exit();
        }
    }


    // ----------------------------------------------------------------------------
    /**
    * CONNECTS TO DOM DOCUMENT
    *
    * creates a DOMDocument using the class var content (filled up with get_xml_content())
    * 
    * @param none
    * @return void
    */
    protected function DOMconnector() {
        try {
            if ( !$this->xml_content ) {
                throw new Exception('No content. Empty page or get_xml_content must be run before.');
            }

            $dom = new DOMDocument();
            // this should stay before load() to format output otherwise it doesn't

            $dom->preserveWhiteSpace = false;
            $dom->loadXML($this->xml_content);
            $dom->formatOutput = TRUE;

            $this->domdoc = $dom;
        } catch (Exception $e) {
            echo 'BNR_reader->DOMconnector: ' .$e->getMessage();
        }
    }

} // class
?>
Return current item: XML Currency Reader