Dispatcher»Sample Code

Sample Code

Dispatcher Sample Code

A dispatcher call can be easily placed inside of a web page, or invoked via simple PHP coding.

HTML/JavaScript - By creating a simple XML string and some JavaScript, NRGship can be easily invoked from any web page. Simply create <Shipment> data in the NRGship XML Schema and load the web page and the data will be passed to NRGship.

<html>
<head>
<script language='javascript' type='text/javascript'>
function sendData() {
var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://IPNUMBER:54242/posttext.html', true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) { //4 means request finished and response is ready
// alert(xhr.responseText);
}
};
var contentType = 'application/json';
xhr.setRequestHeader('Content-Type', contentType);

for (var header in this.headers) {
xhr.setRequestHeader(header, headers[header]);
}

// here's our data variable that we talked about earlier
var data = '<TROI_TEXT_UTF8><NRGship><id>123</id><action>show</action><Shipment>...</Shipment><button></button></NRGship></TROI_TEXT_UTF8>';

// finally send the request as binary data
xhr.send(data);
}
</script>
</head>
<body onLoad='sendData()'>
<input id='subbtn' type='button' value='Send' onclick='sendData()'></input>
</body>
</html>

PHP - By making a CURL request from PHP, you can easily invoke the dispatcher. In the sample below, NRGship will invoke the Ship action for ID 123.

<?php

// set url and data
$url = 'http://ipnumber:54242/posttext.html';
$data = '<TROI_TEXT_UTF8><NRGship><id>123</id><action>ship</action><button></button></NRGship></TROI_TEXT_UTF8>';

// execute request
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_HEADER,1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_TIMEOUT,60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response=curl_exec($ch);
curl_close($ch);

?>