XML opvragen met PHP en extra

Status
Niet open voor verdere reacties.

nkjez

Nieuwe gebruiker
Lid geworden
18 jun 2007
Berichten
1
Hoi,

Ik ben bezig met PHP en XML. Ik heb het nu voor elkaar dat de XML wordt uitgelezen met PHP, maar nu wil ik het volgende bereiken.

De onderstaande PHP code laat dit zien: voorbeeld:

HTML:
id  	title  	link  	description  	price  	country  	area  	start_date  	end_date  	departure_date  	length  	transportation_type  	accomodation_type  	holiday_type  	thumbnail  	photo  	duration  	transport  	persons
1 	Adonis resort Castalia Park/Los Brezos 	[url]http://ds1.nl/c/?wi=47080&si=119&li=12871&dl=vakantie%2F%3Fland%3Dcanarische_eil%26streek%3Dtenerife%26bestemming%3Dcosta_adeje%26acco%3Dadonis_resort_castalia_parklos_brezos%26jumpTo%3D20070831%26duration%3D8%26transport%3DVL%26persons%3D3%26page%[/url] 	Zeer sfeervol en verzorgde appartementen met een gunstige ligging t.o.v. het strand. 	499 	ES 	Costa Adeje 			2007-08-31 					[url]http://images.bizztravel.nl/pics/1171466657.jpg[/url] 	[url]http://images.bizztravel.nl/pics/1171466656.jpg[/url] 	8 	Vliegtuig 	3
2 	Aparthotel Albona 	[url]http://ds1.nl/c/?wi=47080&si=119&li=12871&dl=vakantie%2F%3Fland%3Dkroatie%26streek%3Distrie%26bestemming%3Drabac%26acco%3Daparthotel_albona%26jumpTo%3D20070710%26duration%3D8%26transport%3DVL%26persons%3D3%26page%3Dfh_page%26AFC-affiliates&ws=testXML[/url] 	Dit aparthotel is rustig gelegen, hoog boven zee. 	509 	HR 	Rabac 			2007-07-10 					[url]http://images.bizztravel.nl/pics/1172047986.jpg[/url] 	[url]http://images.bizztravel.nl/pics/1172047985.jpg[/url] 	8 	Vliegtuig 	3
3 	Aparthotel Annalisa 	[url]http://ds1.nl/c/?wi=47080&si=119&li=12871&dl=vakantie%2F%3Fland%3Dgriekenland%26streek%3Dcorfu%26bestemming%3Dipsos%26acco%3Daparthotel_annalisa%26jumpTo%3D20070922%26duration%3D8%26transport%3DVL%26persons%3D3%26page%3Dfh_page%26AFC-affiliates&ws=[/url] 	Sfeervol complex met een rustige ligging en toch maar 150m van het strand en de boulevard. 	249 	GR 	Ipsos 			2007-09-22 					[url]http://images.bizztravel.nl/pics/1171461109.jpg[/url] 	[url]http://images.bizztravel.nl/pics/1171461108.jpg[/url] 	8 	Vliegtuig 	3

Je ziet hier 3 van de ongeveer duizenden resultaten worden weeregeven (als voorbeeld). Nu wil ik uit deze enorme lijst de resultaten gaan selecteren op plaats (in de xml bekend als country en area). Hoe bereik ik dit en welke aanpassingen moet ik maken in php document?

Ik zat ook te denken de xml te transferen in MySQL, maar hier heb ik (nog) geen verstand van. Is dit ook een optie en werkt deze efficient?

Klein deel van de XML file

Code:
<items>
−
	<general>
<merchant_name>Bizztravel zonvakanties</merchant_name>
<channel/>
<refresh_interval>120</refresh_interval>
<expiration_time>2880</expiration_time>
<last_update>2007-06-18 00:15:54</last_update>
<author>www.affiliates.nl</author>
</general>
−
	<item>
<id>1</id>
<title>Adonis resort Castalia Park/Los Brezos</title>
−
	<link>
http://ds1.nl/c/?wi=47080&si=119&li=12871&dl=vakantie%2F%3Fland%3Dcanarische_eil%26streek%3Dtenerife%26bestemming%3Dcosta_adeje%26acco%3Dadonis_resort_castalia_parklos_brezos%26jumpTo%3D20070831%26duration%3D8%26transport%3DVL%26persons%3D3%26page%
</link>
−
	<description>
Zeer sfeervol en verzorgde appartementen met een gunstige ligging t.o.v. het strand.
</description>
<price>499</price>
<country>ES</country>
<area>Costa Adeje</area>
<start_date/>
<end_date/>
<departure_date>2007-08-31</departure_date>
<length/>
<transportation_type/>
<accomodation_type/>
<holiday_type/>
<thumbnail>http://images.bizztravel.nl/pics/1171466657.jpg</thumbnail>
<photo>http://images.bizztravel.nl/pics/1171466656.jpg</photo>
<duration>8</duration>
<transport>Vliegtuig</transport>
<persons>3</persons>
</item>

Mijn php code

PHP:
<?

    

    function showRow(&$row)
    {
        static $head = false;
        if($head === false){
            echo "\t<tr>\n";
            foreach($row as $key=>$value){
                echo "\t\t<td>".$key."</td>\n";
            }
            $head = true;
            echo "</tr>\n";
        }
        echo "\t<tr>\n";
        foreach($row as $key=>$value){
            echo "\t\t<td>".$value."</td>\n";
        }
        echo "\t</tr>\n";
    }
    
    $xml = new xmlFeedDownloader(119, 47080, 1,'testXML');
    $display = new xmlFeedDisplay($xml);
    $display->registerItemFunction('showRow');

    
    echo "<table>";
    $display->getFeed();
    echo "</table>";
    echo "Type:".$xml->state;
    
    class xmlFeedDisplay
    {

        var $currentElement = '';
        var $buffer = '';
        var $itemFunction = '';
        var $item = array();
        var $within = array();
        var $xml_parser = null;
        var $downloader = null;
        
        function xmlFeedDisplay(&$downloader)
        {
            $this->downloader = &$downloader;
            $this->xml_parser = xml_parser_create();
            xml_set_object($this->xml_parser,&$this);
            xml_parser_set_option($this->xml_parser,XML_OPTION_CASE_FOLDING,FALSE);
            xml_set_element_handler($this->xml_parser,'startElement', 'endElement');
            xml_set_character_data_handler($this->xml_parser, 'characterData');
        }
        
        function registerItemFunction($functionName)
        {
            if(function_exists($functionName)){
                $this->itemFunction = $functionName;
                return true;
            }else{
                $this->itemFunction = null;
                return false;
            }
        }
        
        function getFeed()
        {
            $a = implode("\n",$this->downloader->getFeed());
            xml_parse($this->xml_parser,$a ,true);
            xml_parser_free($this->xml_parser);
        }
        
        function startElement($parser, $name, $attrs)
        {
            $this->buffer = '';
            $this->within[$name] = true;
            $this->currentElement = $name;
        }
        
        function endElement($parser,$name)
        {
            switch($name){
                case 'item':
                    if(!is_null($this->itemFunction)){
                        call_user_func($this->itemFunction,$this->item);
                    }
                    $this->item = array();
                    break;
                default:
                    if($this->within['item']==true){
                        $this->item[$name] = trim($this->buffer);
                    }
                    break;
            }
            $this->within[$name] = false;
            
        }
        
        function characterData($parser, $value)
        {
            $this->buffer.= $value;
        }    
    }
    
    
    
    class xmlFeedDownloader
    {
        var $subMerchantId = NULL;
        var $websiteId = NULL;
        var $xmlId = NULL;
        var $websiteSubId = NULL;
        var $fileName = 'xml_feed_';  /*   naam file, belangrijk! */
        var $state = 'nothing';
        
        function xmlFeedDownloader($subMerchantId, $websiteId, $xmlId, $websiteSubId = NULL)
        {
            $this->subMerchantId = $subMerchantId;
            $this->websiteId = $websiteId;
            $this->xmlId = $xmlId;
            $this->websiteSubId = $websiteSubId;
        }
        
        function setTmpPath($path)
        {
            if(substr($path,-1)!='/'){
                $path.'/';
            }
            $this->tmpPath = $path;
            return $this->tmpPath;
        }
        
        function getFeedFileName()
        {
            return $this->tmpPath.$this->fileName.'_'.$this->xmlId.'_'.$this->channelId.'_'.$this->subMerchantId.'.xml';
        }
        
        function getFeed()
        {
            $this->checkLastUpdate();
            return file($this->getFeedFileName());
        }
        
        function checkLastUpdate()
        {
            if(file_exists($this->getFeedFileName())){
                $lastDownload = date('U',fileatime($this->tmpPath.$this->fileName));
                $refresh_interval = preg_replace("/^.+?<items>.+?<general>.+?<refresh_interval>(.+?)<\/refresh_interval>.+?<\/general>.+?<\/items>.+?$/sie", "preg_replace('/[^0-9]+/', '', preg_replace('/[\.|,].+$/s', '', '\\1'))", implode('',file($this->tmpPath.$this->fileName)));
                $validUntil = $lastDownload + ($refresh_interval * 60);
                if($validUntil<time()){
                    $this->state = 'downloading';
                    return $this->downLoadXml();
                }
                $this->state = 'history';
                return true;
            }else{
                $this->state = 'newdownload';
                return $this->downLoadXml();
            }    
        }
        
        function showFeed()
        {
            $this->checkLastUpdate();
            header('Content-type: text/xml');
            readfile($this->getFeedFileName());
        }
        
        function downLoadXml()
        {
            if(!copy($this->getUrl(),$this->getFeedFileName())){
                return false;
            }
        }
        
        function getUrl()
        {
            return 'http://xml.ds1.nl/get/?si='.$this->subMerchantId.'&wi='.$this->websiteId.'&xid='.$this->xmlId.'&ws='.urlencode($this->websiteSubId);
        }
        
        function error($msg)
        {
            die('<b>xmlFeed error:</b> '.$msg);
        }
    }
?>
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan