Jump to content

Getting data from curl imported XML page?


Kristoff1875

Recommended Posts

I'm getting data from a remote XML file using the following as a result of a form:

 

<?php

header('Content-type: text/xml');

//extract data from the post
extract($_POST);

//set POST variables
$url = 'https://www.*******';
$fields = array(
            'ESERIES_FORM_ID'=>urlencode($ESERIES_FORM_ID),
            'MXIN_USERNAME'=>urlencode($MXIN_USERNAME),
            'MXIN_PASSWORD'=>urlencode($MXIN_PASSWORD),
            'MXIN_VRM'=>urlencode($MXIN_VRM),
            'MXIN_TRANSACTIONTYPE'=>urlencode($MXIN_TRANSACTIONTYPE),
            'MXIN_PAYMENTCOLLECTIONTYPE'=>urlencode($MXIN_PAYMENTCOLLECTIONTYPE),
            'MXIN_CAPCODE'=>urlencode($MXIN_CAPCODE)
        );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);


?>

 

Which is displaying what was the remote XML page, but has brought it to my domain.

 

How would I go about taking the results of the displaying XML page and displaying them on the next page rather than just displaying the XML data?

 

Many thanks in advanced!

<GEODS exp_cid="41C2"><REQUEST type="RETURN" subtype="CALLBUR" EXP_ExperianRef="" success="Y" timestamp="Mon, 21 May 2012 at 12:04 PM" id="41C2"><MB01 seq="01"><DATEOFTRANSACTION>20000628</DATEOFTRANSACTION><VRM>W******</VRM><VINCONFIRMATIONFLAG>0</VINCONFIRMATIONFLAG><ENGINECAPACITY>01989</ENGINECAPACITY><DOORPLAN>14</DOORPLAN><DATEFIRSTREGISTERED>20000531</DATEFIRSTREGISTERED><YEAROFMANUFACTURE>2000</YEAROFMANUFACTURE><SCRAPPED>0</SCRAPPED><EXPORTED>0</EXPORTED><IMPORTED>0</IMPORTED><MAKE>FORD</MAKE><MODEL>FOCUS GHIA</MODEL><COLOUR>BLACK</COLOUR><TRANSMISSION>MANUAL 5 GEARS</TRANSMISSION><ENGINENUMBER>XU20092</ENGINENUMBER><VINSERIALNUMBER>WF0AXXGCDAXU20092</VINSERIALNUMBER><DOORPLANLITERAL>5 DOOR HATCHBACK</DOORPLANLITERAL><MVRISMAKECODE>M1</MVRISMAKECODE><MVRISMODELCODE>AJZ</MVRISMODELCODE><DTPMAKECODE>M1</DTPMAKECODE><DTPMODELCODE>868</DTPMODELCODE><TRANSMISSIONCODE>M</TRANSMISSIONCODE><GEARS>5</GEARS><FUEL>PETROL</FUEL><CO2EMISSIONS>*</CO2EMISSIONS><USEDBEFORE1STREG>0</USEDBEFORE1STREG><IMPORTNONEU>0</IMPORTNONEU><UKDATEFIRSTREGISTERED>20000531</UKDATEFIRSTREGISTERED><MAKEMODEL>FORD FOCUS GHIA</MAKEMODEL></MB01><MB12 seq="01"><CAPCODE>FOFO20GH 5HPIM  1</CAPCODE></MB12></REQUEST></GEODS>

 

Here is the XML result

If you save the xml to file

 

<?php
$xml = simplexml_load_file("uat.xml");
$data = (array)$xml->REQUEST->MB01;
array_shift($data);              // remove @attributes

echo "<table>\n" ;
foreach ($data as $k => $v) {
    echo "<tr><td>$k</td><td>$v</td></tr>\n";
}
echo "</table>\n";
?>

 

Or you can use simplexml_load_string ($xmlstring).

Make sure file or string have the xml header line

formaction.php file:

 

<?php


header('Content-type: text/xml');

//extract data from the post
extract($_POST);

//set POST variables
$url = 'https://www.***.com/UAT/';
$fields = array(
            'ESERIES_FORM_ID'=>urlencode($ESERIES_FORM_ID),
            'MXIN_USERNAME'=>urlencode($MXIN_USERNAME),
            'MXIN_PASSWORD'=>urlencode($MXIN_PASSWORD),
            'MXIN_VRM'=>urlencode($MXIN_VRM),
            'MXIN_TRANSACTIONTYPE'=>urlencode($MXIN_TRANSACTIONTYPE),
            'MXIN_PAYMENTCOLLECTIONTYPE'=>urlencode($MXIN_PAYMENTCOLLECTIONTYPE),
            'MXIN_CAPCODE'=>urlencode($MXIN_CAPCODE)
        );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);


?>

 

Source of formaction.php:

 

<?xml version='1.0' standalone='yes'?><GEODS exp_cid='809i'><REQUEST type='RETURN' subtype='CALLBUR' EXP_ExperianRef='' success='Y' timestamp='Mon, 21 May 2012 at 1:48 PM' id='809i'><MB01 seq='01'><DATEOFTRANSACTION>20020725</DATEOFTRANSACTION><VRM>WF02LPZ </VRM><VINCONFIRMATIONFLAG>0</VINCONFIRMATIONFLAG><ENGINECAPACITY>01199</ENGINECAPACITY><DOORPLAN>13</DOORPLAN><DATEFIRSTREGISTERED>20020625</DATEFIRSTREGISTERED><YEAROFMANUFACTURE>2002</YEAROFMANUFACTURE><SCRAPPED>0</SCRAPPED><EXPORTED>0</EXPORTED><IMPORTED>0</IMPORTED><MAKE>VAUXHALL</MAKE><MODEL>CORSA COMFORT 16V SEMI-AU</MODEL><COLOUR>RED</COLOUR><TRANSMISSION>MANUAL 5 GEARS</TRANSMISSION><ENGINENUMBER>19W84037</ENGINENUMBER><VINSERIALNUMBER>W0L0XCF0824252589</VINSERIALNUMBER><DOORPLANLITERAL>3 DOOR HATCHBACK</DOORPLANLITERAL><MVRISMAKECODE>D8</MVRISMAKECODE><MVRISMODELCODE>AHA</MVRISMODELCODE><DTPMAKECODE>DB</DTPMAKECODE><DTPMODELCODE>858</DTPMODELCODE><TRANSMISSIONCODE>M</TRANSMISSIONCODE><GEARS>5</GEARS><FUEL>PETROL</FUEL><CO2EMISSIONS>149</CO2EMISSIONS><USEDBEFORE1STREG>0</USEDBEFORE1STREG><IMPORTNONEU>0</IMPORTNONEU><UKDATEFIRSTREGISTERED>20020625</UKDATEFIRSTREGISTERED><MAKEMODEL>VAUXHALL CORSA COMFORT 16V SEMI-AU</MAKEMODEL></MB01><MB12 seq='01'><CAPCODE>VACO12C1E3HPIA  1</CAPCODE></MB12></REQUEST></GEODS>

You're on your own with this as no-one else can replicate what you are doing, but

 

At beginning of script put

ob_start();

 

and right at the end of the script try

$str = ob_get_contents();
ob_end_clean();

//my code goes here

I was getting errors when adding extra code, assuming it was because of the header being set as xml:

 

header('Content-type: text/xml');

 

So after taking that out and adding your code at the end, i've now got the following error:

 

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "uat.xml" in formaction.php on line 48

 

Is that because it's not saving imported result correctly?

Next question for you now (sorry!!!!) is there a way of taking those values and only showing a selection of them rather than all of them and how would I go about just storing the values and passing them to the next page? Is it a case of sending it via the url or can I hide them by sending them as hidden form fields?

You can be selective

$xml = simplexml_load_string($str);
$make = $xml->REQUEST->MB01->MAKE; 
$model = $xml->REQUEST->MB01->MODEL;

echo "$make $model";                  // --> JEEP GRAND CHEROKEE LIMITED

 

You have options regarding the next page

  • Hidden fields
  • session vars
  • save xml to file

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.