Skip to content

Instantly share code, notes, and snippets.

@liangzan
Created October 8, 2015 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liangzan/8b3e3db573f276d040c8 to your computer and use it in GitHub Desktop.
Save liangzan/8b3e3db573f276d040c8 to your computer and use it in GitHub Desktop.
<?php
/**
* Sample to use Courex XML Technical Guide
*
* How to use:
* Search for "//input" and fill in the data accordingly
*
* @author Tao Yueling <yueling@courex.com.sg>
* @copyright 2014 Courex Pte Ltd
* @version 1.0.0
*/
//------------------------- functions -------------------------//
function xmlRequest($request_xml)
{
$ch = curl_init();
//curl_setopt($ch, CURLOPT_URL, "http://www.courex.com.sg/xml/");//live site//input
curl_setopt($ch, CURLOPT_URL, "http://repo.courex.com.sg:8088/courex.com.sg/xml/");//test site//input
curl_setopt($ch, CURLOPT_POSTFIELDS, "HTTP_RAW_POST_DATA=" . urlencode($request_xml));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
$return_xml = curl_exec($ch);
return $return_xml;
}
//------------------------- End functions -------------------------//
//------------------------- generate xml -------------------------//
$xml = new DOMDocument();
$xml->formatOutput = true;
//mandatory - Create xml elements
$requestCall = $xml->createElement("RequestCall");
$AppType = $xml->createElement("AppType");
$UserName = $xml->createElement("UserName");
$Password = $xml->createElement("Password");
$Parameters = $xml->createElement("Parameters");
//assign nodeValue
//$AppType->nodeValue = 'Tracking';//input
//$AppType->nodeValue = 'Order';//input
$AppType->nodeValue = 'Quote';//input
$UserName->nodeValue = 'CourexUserName';//input
$Password->nodeValue = 'CourexPassword';//input
//create xml elements and assign nodeValue for different type of application
// Application type - Tracking
if($AppType->nodeValue == 'Tracking')
{
//Create xml elements
$params['TrackNo'] = $xml->createElement("TrackNo");
//Assign nodeValue
$params['TrackNo']->nodeValue = 'CO279451';//input
}
// Application type - Order
if($AppType->nodeValue == 'Order')
{
//Create xml elements
$params['OrderType'] = $xml->createElement("OrderType");
$params['SenderName'] = $xml->createElement("SenderName");
$params['SenderCompany'] = $xml->createElement("SenderCompany");
$params['SenderContact'] = $xml->createElement("SenderContact");
$params['SenderAddress'] = $xml->createElement("SenderAddress");
$params['SenderAddress2'] = $xml->createElement("SenderAddress2");
$params['SenderUnit'] = $xml->createElement("SenderUnit");
$params['SenderCity'] = $xml->createElement("SenderCity");
$params['SenderState'] = $xml->createElement("SenderState");
$params['SenderPostalCode'] = $xml->createElement("SenderPostalCode");
$params['SenderCountry'] = $xml->createElement("SenderCountry");
$params['RecipientName'] = $xml->createElement("RecipientName");
$params['RecipientCompany'] = $xml->createElement("RecipientCompany");
$params['RecipientContact'] = $xml->createElement("RecipientContact");
$params['RecipientAddress'] = $xml->createElement("RecipientAddress");
$params['RecipientAddress2'] = $xml->createElement("RecipientAddress2");
$params['RecipientUnit'] = $xml->createElement("RecipientUnit");
$params['RecipientCity'] = $xml->createElement("RecipientCity");
$params['RecipientState'] = $xml->createElement("RecipientState");
$params['RecipientPostalCode'] = $xml->createElement("RecipientPostalCode");
$params['RecipientCountry'] = $xml->createElement("RecipientCountry");
$params['ContentDesc'] = $xml->createElement("ContentDesc");
$params['DeclaredValue'] = $xml->createElement("DeclaredValue");
$params['Weight'] = $xml->createElement("Weight");
$params['ShipMethod'] = $xml->createElement("ShipMethod");
$params['ShipType'] = $xml->createElement("ShipType");
$params['PickupDate'] = $xml->createElement("PickupDate");
$params['PickupTime'] = $xml->createElement("PickupTime");
$params['DeliverDate'] = $xml->createElement("DeliverDate");
$params['DeliverTime'] = $xml->createElement("DeliverTime");
$params['VolL'] = $xml->createElement("VolL");
$params['VolW'] = $xml->createElement("VolW");
$params['VolH'] = $xml->createElement("VolH");
$params['Instruction'] = $xml->createElement("Instruction");
$params['PaymentMethod'] = $xml->createElement("PaymentMethod");
$params['PaymentParty'] = $xml->createElement("PaymentParty");
//Assign nodeValue
$params['OrderType']->nodeValue = 'Local';//input
$params['SenderName']->nodeValue = 'Test Sender Name';//input
$params['SenderCompany']->nodeValue = 'Courex Pte Ltd';//input
$params['SenderContact']->nodeValue = '12345678';//input
$params['SenderAddress']->nodeValue = 'Test Sender Address';//input
$params['SenderAddress2']->nodeValue = '';//input
$params['SenderUnit']->nodeValue = '0000';//input
$params['SenderCity']->nodeValue = 'NA';//input
$params['SenderState']->nodeValue = 'NA';//input
$params['SenderPostalCode']->nodeValue = '499637';//input
$params['SenderCountry']->nodeValue = 'Singapore';//input
$params['RecipientName']->nodeValue = 'test';//input
$params['RecipientCompany']->nodeValue = 'test';//input
$params['RecipientContact']->nodeValue = '87654321';//input
$params['RecipientAddress']->nodeValue = 'Test Recipient Address';//input
$params['RecipientAddress2']->nodeValue = '';//input
$params['RecipientUnit']->nodeValue = '1111';//input
$params['RecipientCity']->nodeValue = 'NA';//input
$params['RecipientState']->nodeValue = 'NA';//input
$params['RecipientPostalCode']->nodeValue = '470726';//input
$params['RecipientCountry']->nodeValue = 'Singapore';//input
$params['ContentDesc']->nodeValue = 'test content';//input
$params['DeclaredValue']->nodeValue = '5';//input
$params['Weight']->nodeValue = '1';//input
$params['ShipMethod']->nodeValue = 'Local';//input
$params['ShipType']->nodeValue = 'SmallParcel';//input
$params['PickupDate']->nodeValue = '25042014';//DDMMYYYY//input
$params['PickupTime']->nodeValue = '1400';//input
$params['DeliverDate']->nodeValue = '26042014';//DDMMYYYY//input
$params['DeliverTime']->nodeValue = '1400';//input
$params['VolL']->nodeValue = '5';//input
$params['VolW']->nodeValue = '5';//input
$params['VolH']->nodeValue = '5';//input
$params['Instruction']->nodeValue = 'Test Instruction';//input
$params['PaymentMethod']->nodeValue = '1';//input
$params['PaymentParty']->nodeValue = '1';//input
}
// Application type - Quote
if($AppType->nodeValue == 'Quote')
{
//Create xml elements
$params['SenderCountry'] = $xml->createElement("SenderCountry");
$params['RecipientCountry'] = $xml->createElement("RecipientCountry");
$params['SenderPostalCode'] = $xml->createElement("SenderPostalCode");
$params['RecipientPostalCode'] = $xml->createElement("RecipientPostalCode");
$params['StandardService'] = $xml->createElement("StandardService");
$params['Weight'] = $xml->createElement("Weight");
//Assign nodeValue
$params['SenderCountry']->nodeValue = 'Singapore';//input
$params['RecipientCountry']->nodeValue = 'Singapore';//input
$params['SenderPostalCode']->nodeValue = '499637'; //input
$params['RecipientPostalCode']->nodeValue = '470726';//input
$params['StandardService']->nodeValue = 'Enable';//input
$params['Weight']->nodeValue = '1';//input
}
//--------------- generate xml output ---------------//
$xml->appendChild( $requestCall );
$requestCall->appendChild( $AppType );
$requestCall->appendChild( $UserName );
$requestCall->appendChild( $Password );
$requestCall->appendChild( $Parameters );
foreach($params as $value) if($value->nodeValue!=='') $Parameters->appendChild( $value );
//save this xml output to an xml file
//$xml->save("test.xml");
echo "Output of your xml(View page source to see the format in xml):<br>\r\n";
print $xml->saveXML();
//--------------- End generate xml output ---------------//
//------------------------- End generate xml -------------------------//
echo "\r\n\r\n<br><br>Output of response xml(View page source to see the format in xml):<br>\r\n";
print xmlRequest($xml->saveXML());
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment