line below.
$custNmbr = "CUSTNMBR";
$today = date("m/d/y");
// array of input data
$myInput = $_GET;
// If you don't pass data in as a GET variables, hard code them here:
if (! isset($myInput['origZip'])) {
$myInput['origZip'] = "19348";
}
if (! isset($myInput['destZip'])) {
$myInput['destZip'] = "97202";
}
// array of elements you want to query from the XML Response
$myElements = array('TotalCharge', 'Discount', 'TotalAccessorialCharges', 'NetCharge', 'TransitTime');
/*
Build XML Request
In actual use, you would probably populate the XML Request
parameters (Weights, Classes, Zip Codes, etc.) from data submitted
via an on-line order form or database.
For this sample we will just hard code some dummy data.
*/
$itemArray = array(); //Your commodity items, maximum of 4 - add as needed
$itemArray[] = array('class'=>'775', 'weight'=>'667');
$itemArray[] = array('class'=>'100', 'weight'=>'555');
$accArray = array(); // Your accessorial services - add as needed
$accArray[] = "SSC";
$accArray[] = "DNC";
$accArray[] = "GUR";
extract($myInput);
$xmlRequest="" .
"$origZip" .
"$destZip";
// To get customer specific discount, uncomment this line:
// $xmlRequest .= "$custNmbr";
$xmlRequest .= "P" .
"100" .
"$today";
foreach ($itemArray as $item) { // Add commodity items to the XML Request
extract($item);
$xmlRequest .= "" .
"$class" .
"$weight" .
"";
}
foreach ($accArray as $acc) { // Add accessorials to the XML Request
$xmlRequest .= "$acc";
}
$xmlRequest .= "";
//Convert characters to proper format for HTTP POST
$xmlRequest = urlencode($xmlRequest);
// open synchronous connection to Con-way servlet and set options
$urlConn = curl_init ($requestUrl);
curl_setopt ($urlConn, CURLOPT_POST, 1);
curl_setopt ($urlConn, CURLOPT_SSL_VERIFYPEER, false); // May be needed for SSL behind a firewall
curl_setopt ($urlConn, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded"));
curl_setopt ($urlConn, CURLOPT_USERPWD, $username.":".$password);
curl_setopt ($urlConn, CURLOPT_POSTFIELDS, "$requestType=$xmlRequest");
// Parse the XML Response
ob_start(); // prevent the buffer from being displayed
curl_exec($urlConn);
$xmlResponse = simplexml_load_string( ob_get_contents() );
ob_end_clean();
curl_close($urlConn); // close the connection
// Build the HTML page
echo "
Sample PHP 5 for Con-way XML $title>
Sample PHP 5 Con-way XML $title
";
// Extract your element values from the XML Response
// The outer loop iterates the total number of occurrences of your first element
for ($i=0; $i < count($xmlResponse->xpath('//'.$myElements[0])); $i++) {
// The inner loop iterates all of your elements, for each occurrence of the first
foreach ($myElements as $myEl) {
$myVals = $xmlResponse->xpath('//'.$myEl);
echo "Value of <$myEl> is: $myVals[$i] ";;
}
echo ' ';
}
echo "Here is the dump of the XML output for $title: ";
echo $xmlResponse->asXML();
echo"
" .
"And here it is displayed as a formatted array: ";
print_r($xmlResponse);
echo
"" .
"";
?>