<?php /* $Id: XML_Order_PHP_sample_code.php,v 1.1 http://www.shipwire.com Copyright (c) 2004-2008 Shipwire.com For use with Shipwire.com only */ // replace the "email" & "passWd" String values with your Shipwire email and password $email = "your@email.com"; $passwd = "yourpassword"; $server = "Test"; // or "Production" $warehouse = "0"; // Leave "00" if you want Shipwire to determine the warehouse /* Build Order Submitter In actual use, you would probably populate the order submission parameters (Address, Item, Quantity, etc.) from data submitted via an on-line order form or database. For this sample we will just hard code some dummy data. */ $OrderList='<OrderList StoreAccountName="scr-test-485.0123456789ABCDEF"> <EmailAddress>'.$email.'</EmailAddress> <Password>'.$passwd.'</Password> <Server>'.$server.'</Server> <Referer>023YAHOO</Referer> <Order id="test-485"> <Warehouse>'.$warehouse.'</Warehouse> <AddressInfo type="ship"> <Name> <Full>Sheridan Rawlins</Full> </Name> <Address1>321 Foo bar lane</Address1> <Address2>Apartment #2</Address2> <City>Nowhere</City> <State>CA</State> <Country>US United States</Country> <Zip>12345</Zip> <Phone>555.444.3210</Phone> <Email>sheridan@rawlins.com</Email> </AddressInfo> <Shipping>GD</Shipping> <Item num="0"> <Code>12345</Code> <Quantity>1</Quantity> </Item> <Item num="1"> <Code>12345</Code> <Quantity>1</Quantity> </Item> </Order> <Order id="scr-test-486"> <Warehouse>'.$warehouse.'</Warehouse> <AddressInfo type="ship"> <Name> <Full>Bob Smith</Full> </Name> <Address1>This Lane</Address1> <Address2>#333</Address2> <City>Bob</City> <State>NV</State> <Country>US United States</Country> <Zip>89110</Zip> <Phone>555.444.3210</Phone> <Email>smith@rawlins.com</Email> </AddressInfo> <Shipping>GD</Shipping> <Item num="0"> <Code>12345</Code> <Quantity>1</Quantity> </Item> </Order> <Order id="scr-test-487"> <Warehouse>'.$warehouse.'</Warehouse> <AddressInfo type="ship"> <Name> <Full>Sheridan Rawlins</Full> </Name> <Address1>321 Foo bar lane</Address1> <Address2>Apartment #2</Address2> <City>Nowhere</City> <State>CA</State> <Country>US United States</Country> <Zip>12345</Zip> <Phone>555.444.3210</Phone> <Email>sheridan@rawlins.com</Email> </AddressInfo> <Shipping>UPS Ground</Shipping> <Item num="0"> <Code>654654</Code> <Quantity>1</Quantity> </Item> </Order> </OrderList>'; //Convert characters to proper format for post $OrderList = urlencode($OrderList); // open synchronous connection to Shipwire servlet // 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 $urlConn = curl_init ("https://api.shipwire.com/exec/FulfillmentServices.php"); curl_setopt ($urlConn, CURLOPT_POST, 1); curl_setopt ($urlConn, CURLOPT_HTTPHEADER, array("Content-type", "application/x-www-form-urlencoded")); curl_setopt ($urlConn, CURLOPT_POSTFIELDS, "OrderListXML=".$OrderList); curl_setopt ($urlConn, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($urlConn, CURLOPT_SSL_VERIFYPEER, 0); $orderSubmitted = curl_exec($urlConn); if (empty($orderSubmitted)) { print "ERROR: " . curl_error($urlConn) . "\n"; exit; } // Parse the response $parser= xml_parser_create(); xml_parse_into_struct($parser,$orderSubmitted,$XMLvals,$XMLindex); 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; } // Use the above function to get the values you want using element names, e.g. $errorMessage = getElementValue($XMLvals,"ErrorMessage"); $totalOrders = getElementValue($XMLvals,"TotalOrders"); $transactionId = getElementValue($XMLvals,"TransactionId"); echo "<HTML> <HEAD> <TITLE>Sample PHP for Shipwire XML Orders</TITLE> </HEAD> <BODY> <CENTER> <B><FONT face=\"arial\" size=+1><I>Sample PHP Shipwire XML Order Submitter</I></FONT></B> </CENTER> <BR>"; // Print the tag values we extracted echo "<b>Here is the data extracted from the Shipwire response XML:</b><br><br> "; if (!empty($errorMessage)) { foreach ($errorMessage as $key => $err) { echo "<B>Error Message: </B>".$err."<BR>"; } } if (!empty($totalOrders)) { foreach ($totalOrders as $key => $tot) { echo "<B>Total Orders: </B>".$tot."<BR>"; } } if (!empty($transactionId)) { foreach ($transactionId as $key => $tra) { echo "<B>Transaction ID: </B>".$tra."<BR>"; } } echo "</BODY></HTML>"; ?>