<?php /* Con-way XML Sample Code for PHP - Bill Of Lading interface This code has been tested on PHP 4.3.XX and PHP 5.XX ----------------- TODO: replace the USERNAME and PASSWORD String values below with your Con-way username and password NOTE: you must have the cURL libraries installed with PHP on your server-- If you need them, see your System Administrator, who can get then at http://curl.haxx.se/download.html ----------------- Send questions to Con-way XML Support at xmlsupport@Con-way.com */ $title = "Bill of Lading"; $requestType = "BOLrequest"; $requestUrl = "https://www.Con-way.com/XMLj/X-BOL"; // replace the USERNAME and PASSWORD String values below with your Con-way username and password $username = "USERNAME"; $password = "PASSWORD"; $today = date("m/d/y"); /* Array of input data - these can be passed in as GET variables * To pass in multiple value, e.g. PO Numbers, pass them in a comma-separted, e.g. * http://yourserver/thisfile.php?poNmbrs=3338889,3338890,3338891 */ $myInput = $_GET; // If you don't pass data in as a GET variables, hard code them here: if (! isset($myInput['shipcode'])) $myInput['shipcode'] = 'S'; if (! isset($myInput['proNmbr'])) $myInput['proNmbr'] = ''; if (! isset($myInput['poNmbrs'])) $myInput['poNmbrs'] = '3338889,3338890'; if (! isset($myInput['consigneeNum'])) $myInput['consigneeNum'] = "883885"; if (! isset($myInput['accessorials'])) $myInput['accessorials'] = 'GUR,DID,DST'; // array of elements you want to query from the XML Response $myElements = array('Status', 'Shipper', 'Consignee', 'FreightAssemblyCenterCode'); /* Build Bill of Lading Request XML In actual use, you would probably populate the BOL 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. */ extract($myInput); $xmlRequest="<BOLrequest testmode=\"Y\"> <RequesterUserId shipcode=\"$shipcode\">$username</RequesterUserId> <ChargeCode>P</ChargeCode> <PRONmbr>$proNmbr</PRONmbr> <CustRefNmbrs>"; foreach ( explode(',',$poNmbrs) as $poNmbr) $xmlRequest .= "<PurchaseOrderNmbr>$poNmbr</PurchaseOrderNmbr>"; $xmlRequest .= "<OtherRefNmbr refcode=\"SKU\" refdesc=\"SKU Number\">3213A</OtherRefNmbr> <OtherRefNmbr refcode=\"UPC\" refdesc=\"UPC number\">789283</OtherRefNmbr> </CustRefNmbrs> <Shipper> <ShipperName>Alan Shipley</ShipperName> <ShipperAddr>1234 NE Main</ShipperAddr> <ShipperCity>Portland</ShipperCity> <ShipperState>OR</ShipperState> <ShipperZip country=\"US\">97202</ShipperZip> <ShipperPhone extension=\"6055\">503.450.6055</ShipperPhone> <ShipperEmail>vonderluft.andrew@Con-way.com</ShipperEmail> </Shipper> <Consignee> <ConsigneeCustNmbr>$consigneeNum</ConsigneeCustNmbr> <ConsigneePhone extension=\"6666\">503.450.6800</ConsigneePhone> <ConsigneeEmail>vonderluft.andrew@Con-way.com</ConsigneeEmail> </Consignee> <Item> <Quantity pkgtype=\"PLT\">44</Quantity> <Weight unit=\"lbs\">667</Weight> <Description>widget-arms</Description> <CmdtyClass>775</CmdtyClass> <NMFClass></NMFClass> <HazMat>N</HazMat> </Item>"; foreach ( explode(',',$accessorials) as $acc) $xmlRequest .= "<Accessorial chargecode=\"P\">$acc</Accessorial>"; $xmlRequest .= "<ShippingRemarks>TEST TEST TEST</ShippingRemarks> <EmergencyContact></EmergencyContact> <PickupRequest> <PickupDate>".$today."</PickupDate> <PickupReadyTime>4:00 pm</PickupReadyTime> <DockCloseTime>7:00 pm</DockCloseTime> <ContactName>Frank</ContactName> <ContactCompany>Franklin Arms</ContactCompany> <ContactPhone>(333)444-4321</ContactPhone> </PickupRequest> </BOLrequest>"; //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"); // Get the XML Response ob_start(); // prevent the buffer from being displayed curl_exec($urlConn); $xmlResponse = ob_get_contents(); ob_end_clean(); curl_close($urlConn); // close the connection // Parse the XML Response $parser= xml_parser_create(); xml_parse_into_struct($parser,$xmlResponse,$xmlVals); xml_parser_free($parser); // This function will return an array of the values of an element // given the $vals and $index arrays, and the element name function getElementValue($xmlVals, $elName) { $elValue = null; foreach ($xmlVals as $arrkey => $arrvalue) { foreach ($arrvalue as $key => $value) { if ($value==strtoupper($elName)){ $elValue[] = $arrvalue['value']; } } } return $elValue; } // Build the HTML page echo " <html> <head> <title>Sample PHP for Con-way XML $title></title> <script language=\"Javascript\" type=\"text/javascript\"> <!-- function erase(object) { object.value=\"\"; } // --> </script> </head> <body text=\"SlateBlue\"> <center> <B><font face=\"arial\" size=+1><I>Sample PHP Con-way XML $title</I></font></B> </center> <br> <form action=\"". $_SERVER['PHP_SELF'] ."\" method=\"get\"> <table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"> "; foreach ($myInput as $key=>$value) { if ($key != "Submit") echo " <tr> <td><b>" . strtoupper($key) . ":&nbsp;&nbsp;</b></td> <td><input type=\"text\" name=\"$key\" value=\"$value\" onFocus=\"erase(this);\" onClick=\"erase(this);\"></td> </tr> "; } echo " <tr><td>&nbsp;</td><td>&nbsp;</td></tr> <tr> <td>&nbsp;</td> <td><input type=\"submit\" name=\"Submit\" value=\"Process $title\"></td> </tr> </table> </form> <hr> "; // Extract your element values from the XML Response $elCount = count(getElementValue($xmlVals, $myElements[0])); // total occurrences // The outer loop iterates the total number of occurrences of your first element for ($i=0; $i < $elCount; $i++) { // The inner loop iterates all of your elements, for each occurrence of the first foreach ($myElements as $myEl) { $myVals = getElementValue($xmlVals, $myEl); echo "<b>Value of &lt;$myEl&gt; is: $myVals[$i]</b><br>";; } echo '<br>'; } echo "<b>Here is the dump of the XML output for $title:</b><br>" . "<code>$xmlResponse</code><br><br>" . "<b>And here it is displayed as a formatted array:</b><br>"; print_r($xmlVals); echo "</body>" . "</html>"; ?>