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
1
<?php
2
$data = array('key' => 'YOUR API KEY', 'includeItemsWithQuantityZero' => false);
3
$data_string = json_encode($data);
4
$context = stream_context_create(array(
5
'http' => array(
6
'method' => "POST",
7
'header' => "Accept: application/json\r\n".
8
"Content-Type: application/json\r\n",
9
'content' => $data_string
10
)
11
));
12
13
$result = file_get_contents('http://user.traxia.com/app/api/inventory', false, $context);
14
var_dump($result);
15
?>
Copied!
Listing changes in inventory based on a UNIX-style timestamp (Milliseconds since the Epoch)
1
<?php
2
$modifiedSince = strtotime('07/28/2014 6:10 GMT') * 1000;
3
$data = array('key' => 'YOUR API KEY', 'modifiedSince' => $modifiedSince, 'includeInactiveItems' => true, 'includeItemsWithQuantityZero' => true);
4
$data_string = json_encode($data);
5
$context = stream_context_create(array(
6
'http' => array(
7
'method' => "POST",
8
'header' => "Accept: application/json\r\n".
9
"Content-Type: application/json\r\n",
10
'content' => $data_string
11
)
12
));
13
14
$result = file_get_contents('http://user.traxia.com/app/api/inventory', false, $context);
15
16
var_dump($result);
17
?>
Copied!
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
1
<?php
2
3
//Call after file_get_contents
4
5
$lastError = error_get_last();
6
if (is_null($lastError)) {
7
// file_get_contents worked successfully
8
} else { // We failed, see the contents of the error
9
var_dump($lastError);
10
}
11
12
?>
Copied!

Using SSL (Recommended)

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.
1
extension=php_openssl.dll ; For Windows
2
extension=php_openssl.so ; For UNIX
Copied!

Other Helpful PHP Resources

Last modified 1mo ago