How to parse xml response from web services

I have been working on developing a website that will grab rates from several other web sites, in xml format, using http post via cURL. I am extremely new to php and coding in general as I was just a network admin before embarking on this project. I am needing help in regards to actually formatting the xml response/s. If I simply echo $response then I get a page that displays the correct information but is all over the place as far as layout is concerned. How do I parse out just the rate/s along w/ a couple other fields, and then format them according to my websites CSS design. I have been looking into simplexml and DOM but just can’t seem to get it to work. I am vaguely familiar with arrays and I am guessing that I will need to parse an array from the string but not exactly sure how to go about it. I am posting my code below, it currently only calls on one webservice. I am also pasting in the xml structure provided by the website I am posting the request to. Let me thank everyone in advance who takes the time to help me out.

This is the php script that receives the post from my html form and then forwards the request to the webservice via cURL:

Untitled Document <?php //fields posted from html form $username=$_POST['Username']; $Password=$_POST['Password']; $CustomerAccount=$_POST['CustomerAccount']; $Option=$_POST['Option']; $returnx=$_POST['returnX']; $rateXML=$_POST['rateXML']; $PickupDateMM=$_POST['PickupDateMM']; $PickupDateDD=$_POST['PickupDateDD']; $PickupDateYYYY=$_POST['PickupDateYYYY']; $Terms=$_POST['Terms']; $EmailAddress=$_POST['EmailAddress']; $OriginCity=$_POST['OriginCity']; $OriginState=$_POST['OriginState']; $OriginZip=$_POST['OriginZip']; $OrigCountry=$_POST['OrigCountry']; $DestinationCity=$_POST['DestinationCity']; $DestinationState=$_POST['DestinationState']; $DestinationZip=$_POST['DestinationZip']; $DestCountry=$_POST['DestCountry']; $Class1=$_POST['Class1']; $Weight1=$_POST['Weight1']; $Description1=$_POST['Description1']; //create array of data to be posted $post_data['Username'] = $username; $post_data['Password'] = $Password; $post_data['CustomerAccount'] = $CustomerAccount; $post_data['Option'] = 'T'; $post_data['returnx'] = $returnx; $post_data['ratexml'] = $rateXML; $post_data['PickupDateMM'] =$PickupDateMM; $post_data['PickupDateDD'] =$PickupDateDD; $post_data['PickupDateYYYY']=$PickupDateYYYY; $post_data['Terms']=$Terms; $post_data['EmailAddress']=$EmailAddress; $post_data['OriginCity']=$OriginCity; $post_data['OriginState']=$OriginState; $post_data['OriginZip']=$OriginZip; $post_data['OrigCountry']=$OrigCountry; $post_data['DestinationCity']=$DestinationCity; $post_data['DestinationState']=$DestinationState; $post_data['DestinationZip']=$DestinationZip; $post_data['DestCountry']=$DestCountry; $post_data['Class1']=$Class1; $post_data['Weight1']=$Weight1; $post_data['Description1']=$Description1; //traverse array and prepare data for posting foreach ( $post_data as $key => $value) { $post_items[] = $key . '=' . $value; } //create the final string to be posted using implode() $post_string = implode ('&', $post_items); //create cURL connection $curl_connection = curl_init('https://www.sefl.com/webconnect/ratequotes'); //set options curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"); curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1); //set data to be posted curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string); //perform our request $result = curl_exec($curl_connection); echo $result; //close the connection curl_close($curl_connection); ?>

Here are the fields that are posted to the external URL:

Submit request to -> https://www.sefl.com/webconnect/ratequotes
Username
Password
CustomerAccount
returnX set to “Y” to get back data in XML format
rateXML set to “Y”
CustomerName
CustomerStreet
CustomerCity
CustomerState
CustomerZip
Option
PickupDateMM
PickupDateDD
PickupDateYYYY
Terms
EmailAddress
OriginCity
OriginState
OriginZip
OrigCountry
DestinationCity
DestinationState
DestinationZip
DestCountry
Class1 thru Class20
Weight1 thru Weight20
CubicFt1 thru CubicFt20
Description1 thru Description20

Rate Quote XML output:

TAG NAME DESCRIPTION
error Any error messages that may cause the quote information to not be returned
quoeteId The quote confirmation number
pickupDate Pickup date requested for quote (does not generate a pickup request)
custAccount Customer account code of party requesting quote (following cust* info applies to this account)
custName Customer name
custAddress Customer Address
custCity Cutomer City
custZip Customer Zip
origCity Shipment origin city
origState Shipment origin state
origZip Shipment origin zip code
origDIP Value will be D for a directly serviced origin point, I for Interline service,
origPhone Origin terminal phone number
destCity Shipment destination city
destState Shipment destination state
destZip Shipment destintion zip code
destDIP Value will be D for a directly serviced destintation point, I for Interline service,
destPhone Destination terminal phone number
quoteDIP Will be D if both origin and destination points are directly serviced, I if one point
transitMiles Miles the shipment will travel, may or may not contain the interline miles
transitTime Trasit Time for the shipment
details

The fields under the detail tag are the detail lines for the quote normally found on the jsp page when requesting a quote via the web page:

typeCharge P for prepaid, C for Collect, T for third Party
description May include items such as “MIN FRT CHG”, “DISCOUNT”, “NET FRT CHG”, etc
weight Weight of shipment, or blank for some detail lines if not applicable
class Class of shipments, or blank if not applicable
rate cwt rate
charges charges applicable for this detail line entry

rateQuote Final quote amount
discountPercent discount applied to shipment
discountAmount discount value
specInstr Any special notations that affect/may affect your pricing/quote (i.e. exceptions, truckloads, import, export, etc).

Sample XML output:

<?xml version="1.0" ?>
CWGGX0933 07/24/2007 001999999 DEMO DEMO ADDR #2 ATLANTA GA 30301 ATLANTA GA D FOSTER CITY CA 94404 P P 2470 4 -
P 100 50 MFC MIN FRT CHG 384.62 D6000 60.00% DISCOUNT 230.77 NFC NET FRT CHG 153.85 AN ARRIVAL NTC 13.00 FS LTL FUEL SURCHARGE OF 18.00% 27.69 TTL 100 194.54
194.54 60.00% 230.77

Simplexml seems to be working just fine. In the example below I used your output but if you load your $result then you should be able to get the info you need and put that in your HTML output.

[php]<?php
$result = <<<EOXML

<?xml version="1.0" ?>
	<root>
	<error />
	<quoteId>CWGGX0933</quoteId>
	<pickupDate>07/24/2007</pickupDate>
	<custAccount>001999999</custAccount>
	<custName>DEMO</custName>
	<custAddress>DEMO ADDR #2</custAddress>
	<custAddress2 />
	<custCity>ATLANTA</custCity>
	<custState>GA</custState>
	<custZip>30301</custZip>
	<origCity>ATLANTA</origCity>
	<origState>GA</origState>
	<origDIP>D</origDIP>
	<destCity>FOSTER CITY</destCity>
	<destState>CA</destState>
	<destZip>94404</destZip>
	<customerZipExt />
	<destDIP>P</destDIP>
	<quoteDIP>P</quoteDIP>
	<transitMiles>2470</transitMiles>
	<transitTime>4</transitTime>
	<details>
	<typeCharge>P</typeCharge>
	<description />
	<weight>100</weight>
	<class>50</class>
	<rate />
	<charges />
	<typeCharge>MFC</typeCharge>
	<description>MIN FRT CHG</description>
	<weight />
	<class />
	<rate />
	<charges>384.62</charges>
	<typeCharge>D6000</typeCharge>
	<description>60.00% DISCOUNT</description>
	<weight />
	<class />
	<rate />
	<charges>230.77</charges>
	<typeCharge>NFC</typeCharge>
	<description>NET FRT CHG</description>
	<weight />
	<class />
	<rate />
	<charges>153.85</charges>
	<typeCharge>AN</typeCharge>
	<description>ARRIVAL NTC</description>
	<weight />
	<class />
	<rate />
	<charges>13.00</charges>
	<typeCharge>FS</typeCharge>
	<description>LTL FUEL SURCHARGE OF 18.00%</description>
	<weight />
	<class />
	<rate />
	<charges>27.69</charges>
	<typeCharge>TTL</typeCharge>
	<description />
	<weight>100</weight>
	<class />
	<rate />
	<charges>194.54</charges>
	</details>
	<rateQuote>194.54</rateQuote>
	<discountPercent>60.00%</discountPercent>
	<discountAmount>230.77</discountAmount>
	</root>

EOXML;

$xml = simplexml_load_string($result);

echo '<pre>';
print_r($xml);
echo '</pre>';

?>[/php]

Richard, thank you for your help. However, when I try your suggestion using the following code:

[code]<?php
//fields posted from html form
$username=$_POST[‘Username’];
$Password=$_POST[‘Password’];
$CustomerAccount=$_POST[‘CustomerAccount’];
$Option=$_POST[‘Option’];
$returnx=$_POST[‘returnX’];
$rateXML=$_POST[‘rateXML’];
$PickupDateMM=$_POST[‘PickupDateMM’];
$PickupDateDD=$_POST[‘PickupDateDD’];
$PickupDateYYYY=$_POST[‘PickupDateYYYY’];
$Terms=$_POST[‘Terms’];
$EmailAddress=$_POST[‘EmailAddress’];
$OriginCity=$_POST[‘OriginCity’];
$OriginState=$_POST[‘OriginState’];
$OriginZip=$_POST[‘OriginZip’];
$OrigCountry=$_POST[‘OrigCountry’];
$DestinationCity=$_POST[‘DestinationCity’];
$DestinationState=$_POST[‘DestinationState’];
$DestinationZip=$_POST[‘DestinationZip’];
$DestCountry=$_POST[‘DestCountry’];
$Class1=$_POST[‘Class1’];
$Weight1=$_POST[‘Weight1’];
$Description1=$_POST[‘Description1’];

//create array of data to be posted
$post_data[‘Username’] = $username;
$post_data[‘Password’] = $Password;
$post_data[‘CustomerAccount’] = $CustomerAccount;
$post_data[‘Option’] = ‘T’;
$post_data[‘returnx’] = $returnx;
$post_data[‘ratexml’] = $rateXML;
$post_data[‘PickupDateMM’] =$PickupDateMM;
$post_data[‘PickupDateDD’] =$PickupDateDD;
$post_data[‘PickupDateYYYY’]=$PickupDateYYYY;
$post_data[‘Terms’]=$Terms;
$post_data[‘EmailAddress’]=$EmailAddress;
$post_data[‘OriginCity’]=$OriginCity;
$post_data[‘OriginState’]=$OriginState;
$post_data[‘OriginZip’]=$OriginZip;
$post_data[‘OrigCountry’]=$OrigCountry;
$post_data[‘DestinationCity’]=$DestinationCity;
$post_data[‘DestinationState’]=$DestinationState;
$post_data[‘DestinationZip’]=$DestinationZip;
$post_data[‘DestCountry’]=$DestCountry;
$post_data[‘Class1’]=$Class1;
$post_data[‘Weight1’]=$Weight1;
$post_data[‘Description1’]=$Description1;

//traverse array and prepare data for posting
foreach ( $post_data as $key => $value) {
$post_items[] = $key . ‘=’ . $value;
}

//create the final string to be posted using implode()
$post_string = implode (’&’, $post_items);

//create cURL connection
$curl_connection =
curl_init(‘https://www.sefl.com/webconnect/ratequotes’);

//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,
“Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)”);
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);

//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

//perform our request
$result = curl_exec($curl_connection);

//close the connection
curl_close($curl_connection);

$xml = simplexml_load_string($result);
echo ‘

’;
print_r($xml)
echo ‘
’;

?>[/code]
Then I get a page full of errors that look like this: error on line 39 at column 46: xmlParseEntityRef: no name, each referencing different lines. So I went back to my form, changed the action to post directly to the url, viewed the source code for the response and it was strictly the xml output I showed in the original posting. I then changed the action to post back to my php script and changed it to only echo $result without the simplexml bit you provided. When viewing the source code behind that response it appears to be an xhtml page. So, when posting directly to the url, it provides a well formed xml response, but when I use cURL to execute the post, I get the following response in xhtml:

[code]

Untitled Document Southeastern Freight Lines | Rate Quote @media print { #aDiv { text-align: left; } #print {position: absolute; left: 0px; width: 650px; top:0px; visibility: visible;} #noprint {display: none; } @page {} } var MENU_ITEMS = [ ['ABOUT SEFL', '/seflWebsite/about/history.jsp', null, ['Our History', '/seflWebsite/about/history.jsp'], ['Quality Process', '/seflWebsite/about/process.jsp'], ['Quality Awards', '/seflWebsite/about/awards.jsp'], ['Service Centers & Transit Maps', '/seflWebsite/servlet?action=svcCtrInfo_getSvcCtrData'], ['Holiday Schedule', '/seflWebsite/about/schedule.jsp'] ], ['WHAT\'S NEW', null, null, ['Press Releases', '/seflWebsite/new/releases.jsp'], ['Fuel Surcharge', '/seflWebsite/new/fuelsurcharge.jsp'], ['Eagle Eye News', '/vspdfdocs/EagleEye.pdf'], ['Customer Testimonials', '/seflWebsite/new/testimonials.jsp'], ['Frequently Asked Questions', '/seflWebsite/new/FAQ.jsp'] ], ['SERVICES', null, null, ['Special Services', '/seflWebsite/services/special.jsp'], [' National Partnerships', '/seflWebsite/services/partners.jsp', null, ['A. Duie Pyle', '/vspdfdocs/PyleProfile.pdf'], ['Central Arizona Freight', '/vspdfdocs/cazfProfile.pdf'], ['Dayton Freight', '/vspdfdocs/DaytonFreight.pdf'], ['Honolulu Freight Service', '/seflWebsite/services/partners_Honolulu.jsp'], [/code] It goes on from here but you get the idea. Any suggestions are greatly appreciated.

I was able to resolve my issue with the xhtml. I am now able to use simplexml to parse the response and display the data that I choose. I am now trying to figure out the best way to handle the results. Should I pass the job of displaying all of the rates to a new script or just echo what I want in the same script that contains the curl request? Basically, now that I can get the data I want to show, what is the best way to format it?

Sponsor our Newsletter | Privacy Policy | Terms of Service