1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP Loop script which gets info from an XML array into PHP variables?

Discussion in 'PHP' started by jeanmichel, Mar 31, 2010.

  1. #1
    Hi,

    Im quite experienced with php, but my working knowledge of xml and how to process it is absolute twoddle!

    Here's the situation;

    Ive got a script which returns raw XML from a remote server, the information needs to be parsed using a PHP function to extract the data into variables/array.

    Here is my code;

    
    <?php
    function postXMLtoURL($server, $path, $xmlDocument) {
    $xmlSource = $xmlDocument;
    $contentLength = strlen($xmlSource);
    $fp = fsockopen($server);
    fputs($fp, "POST $path HTTP/1.0\r\n");
    fputs($fp, "Host: $server\r\n");
    fputs($fp, "Content-Type: text/xml\r\n");
    fputs($fp, "Content-Length: $contentLength\r\n");
    fputs($fp, "Connection: close\r\n");
    fputs($fp, "\r\n"); // all headers sent
    fputs($fp, $xmlSource);
    $result = '';
    while (!feof($fp)) {
    $result .= fgets($fp, 128);
    }
    return $result;
    }
    
    function getBody($httpResponse) {
    $lines = preg_split('/(\r\n|\r|\n)/', $httpResponse);
    $responseBody = '';
    $lineCount = count($lines);
    for ($i = 0; $i < $lineCount; $i++) {
    if ($lines[$i] == '') {
    break;
    }
    }
    for ($j = $i + 1; $j < $lineCount; $j++) {
    $responseBody .= $lines[$j] . "\n";
    }
    return $responseBody;
    }
    
    //$xmlDocument = simplexml_load_file('xml.xml');
    
    $xmlDocument = '<?xml version="1.0" encoding="UTF-8"?>
    <OTA_VehAvailRateRQ
    	xmlns="http://www.opentravel.org/OTA/2003/05"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.opentravel.org/OTA/2003/05 OTA_VehAvailRateRQ.xsd"
    	Target="Test"
    	Version="1.005">
    	<POS>
    		<Source ISOCurrency="GBP">
    			<RequestorID Type="16" ID="999999" ID_Context="CARTRAWLER" />
    		</Source>
    	</POS>
    	<VehAvailRQCore Status="Available">
    		<VehRentalCore PickUpDateTime="2010-04-02T07:00:00" ReturnDateTime="2010-04-09T19:00:00">
    			<PickUpLocation CodeContext="CARTRAWLER" LocationCode="71" />
    			<ReturnLocation CodeContext="CARTRAWLER" LocationCode="71" />
    		</VehRentalCore>
    <DriverType Age=\'30\'/>
    	</VehAvailRQCore>
    <VehAvailRQInfo PassengerQty=\'3\'>
    <Customer>
    <Primary>
    <CitizenCountryName Code=\'IE\' />
    </Primary>
    </Customer>
    <TPA_Extensions>
    <ConsumerIP>255.255.255.255</ConsumerIP>
    </TPA_Extensions>
    </VehAvailRQInfo>
    </OTA_VehAvailRateRQ>';
    
    $result = postXMLtoURL("otatest.cartrawler.com:20002", "/cartrawlerota", $xmlDocument);
    
    $responseBody = getBody($result);
    
    $resultDocument = new DOMDocument();
    $resultDocument->loadXML($responseBody);
    
    header('Content-Type: text/xml');
    print_r($responseBody);
    ?>
    
    PHP:
    And the response i get formatted in XML can be found at ;

    http://www.excessprotection.co.uk/sendxml.php

    Is there a nice PHP function which will parse this huge amount of data into a manageable array as such?
     
    jeanmichel, Mar 31, 2010 IP
  2. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150