PHP Examples
The following examples uses file_get_contents(), which is supported by PHP natively. You may also use curl, but you need to install it on your server first.Listing all Inventory for initial data load
<?php
$data = array('key' => 'YOUR API KEY', 'includeItemsWithQuantityZero' => false);
$data_string = json_encode($data);
$context = stream_context_create(array(
'http' => array(
'method' => "POST",
'header' => "Accept: application/json\r\n".
"Content-Type: application/json\r\n",
'content' => $data_string
)
));
$result = file_get_contents('http://user.traxia.com/app/api/inventory', false, $context);
var_dump($result);
?>
Listing changes in inventory based on a UNIX-style timestamp (Milliseconds since the Epoch)
<?php
$modifiedSince = strtotime('07/28/2014 6:10 GMT') * 1000;
$data = array('key' => 'YOUR API KEY', 'modifiedSince' => $modifiedSince, 'includeInactiveItems' => true, 'includeItemsWithQuantityZero' => true);
$data_string = json_encode($data);
$context = stream_context_create(array(
'http' => array(
'method' => "POST",
'header' => "Accept: application/json\r\n".
"Content-Type: application/json\r\n",
'content' => $data_string
)
));
$result = file_get_contents('http://user.traxia.com/app/api/inventory', false, $context);
var_dump($result);
?>
Don't forget to parse $http_response_header
This StackOverflow answer gives an example of the output of $http_response_header. You should look for or parse for the "200" HTTP status code, or alternatively use php's error_get_last() to see if file_get_contents threw an error.Error handling
<?php
//Call after file_get_contents
$lastError = error_get_last();
if (is_null($lastError)) {
// file_get_contents worked successfully
} else { // We failed, see the contents of the error
var_dump($lastError);
}
?>
If you want to use SSL with file_get_contents, you need to enable the php_openssl extension. To do that, you must edit your php.ini file and add the proper line to the Dynamic Extensions section.
extension=php_openssl.dll ; For Windows
extension=php_openssl.so ; For UNIX
Last modified 1yr ago