Dispatcher»Sample Code

Sample Code

Dispatcher Sample Code

A dispatcher call can be easily placed inside of a web viewer component in FileMaker Pro, or invoked via simple PHP coding.

Web Viewer - The UPS Integration Sample includes a demo showing how to make a request from a web viewer component. First, you must adjust the IP number of your desktop by going to the Webview layout and entering the desktop IP number. Once complete, you can use the script Button - Ship from NRG UPS from iOS to set the web view component and invoke the remote dispatcher.

How does it work? The script Button - Ship from NRG UPS from iOS first creates a related record in the NRGship file. It then calls the dispatchers Ship action and passes the ID value of the related record. In the script step below, two fields are replaced with data from FileMaker - the remote host's IP number and the ID of the related UPS record. When the web viewer is displayed, it runs JavaScript which performs a HTTP POST to the dispatcher.

Set Variable [ $$data; Value: "data:text/html,<html>
<head>
<script language='javascript' type='text/javascript'>
function sendData() {
var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://" & Constant::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>" & UPS_Ship::__kp_Ship_ID & "</id><action>ship</action><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);

?>