Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP example Snippets for API methods #1

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
58 changes: 58 additions & 0 deletions php/getArticlesDelta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* This example script gets information on products and on its related articles from the Tradebyte REST API.
*
* @author Marcos Doellerer <marcos.doellerer@fatchip.de>
*/

/*
* API-Credentials - modify to your personal access credentials
*
* @see https://tradebyte.io/how-to/generate-rest-api-credentials-in-tb-one/
*/
$sApiUser = 'api-user'; // Your API username
$sApiPassword = 'api-password'; // Your API password
$sMerchantId = '1234'; // Your digit merchant ID
$sChannelId = '5678'; // Your digit channel ID

/*
* Get data from REST API with curl
*/

$iDelta = time() - (3600 * 12); //Get the delta for last 12 hours (current time minus 12 hours)
$sUrl = "https://rest.trade-server.net/" . $sMerchantId . "/products/?channel=" . $sChannelId . '&delta=' . $iDelta;

$oCurl = curl_init();
curl_setopt($oCurl, CURLOPT_URL, $sUrl);
curl_setopt($oCurl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($oCurl, CURLOPT_USERPWD, $sApiUser . ":" . $sApiPassword);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($oCurl, CURLOPT_HEADER, 0);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($oCurl, CURLOPT_TIMEOUT, 3600);
$sResponse = curl_exec($oCurl);
if ($sResponse === false) {
echo 'Error: ' . curl_error($oCurl) . ' ErrorNr: ' . curl_errno($oCurl);
}
curl_close($oCurl);

/*
* Process data as you need
*/
$oXml = simplexml_load_string($sResponse);
if ($oXml && $oXml->PRODUCTDATA) {
if ($oXml->SUPPLIER) {
echo "Supplier: " . $oXml->SUPPLIER->NAME . PHP_EOL;
}

foreach ($oXml->PRODUCTDATA->PRODUCT as $oProduct) {
echo "Product n° " . (string)$oProduct->P_NR . " title: " . (string)$oProduct->P_NAME->VALUE . PHP_EOL;

foreach ($oProduct->ARTICLEDATA->ARTICLE as $oArticle) {
echo "Article n° " . (string)$oArticle->A_NR . " ean: " . (string)$oArticle->A_EAN . PHP_EOL;
}
}
} else {
print_r($sResponse);
}
55 changes: 55 additions & 0 deletions php/getArticlesFull.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* This example script gets information on products and on its related articles from the Tradebyte REST API.
*
* @author Marcos Doellerer <marcos.doellerer@fatchip.de>
*/

/*
* API-Credentials - modify to your personal access credentials
*
* @see https://tradebyte.io/how-to/generate-rest-api-credentials-in-tb-one/
*/
$sApiUser = 'api-user'; // Your API username
$sApiPassword = 'api-password'; // Your API password
$sMerchantId = '1234'; // Your digit merchant ID
$sChannelId = '5678'; // Your digit channel ID

/*
* Get data from REST API with curl
*/
$sUrl = "https://rest.trade-server.net/" . $sMerchantId . "/products/?channel=" . $sChannelId;
$oCurl = curl_init();
curl_setopt($oCurl, CURLOPT_URL, $sUrl);
curl_setopt($oCurl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($oCurl, CURLOPT_USERPWD, $sApiUser . ":" . $sApiPassword);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($oCurl, CURLOPT_HEADER, 0);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($oCurl, CURLOPT_TIMEOUT, 3600);
$sResponse = curl_exec($oCurl);
if ($sResponse === false) {
echo 'Error: ' . curl_error($oCurl) . ' ErrorNr: ' . curl_errno($oCurl);
}
curl_close($oCurl);

/*
* Process data as you need
*/
$oXml = simplexml_load_string($sResponse);
if ($oXml && $oXml->PRODUCTDATA) {
if ($oXml->SUPPLIER) {
echo "Supplier: " . $oXml->SUPPLIER->NAME . PHP_EOL;
}

foreach ($oXml->PRODUCTDATA->PRODUCT as $oProduct) {
echo "Product n° " . (string)$oProduct->P_NR . " title: " . (string)$oProduct->P_NAME->VALUE . PHP_EOL;

foreach ($oProduct->ARTICLEDATA->ARTICLE as $oArticle) {
echo "Article n° " . (string)$oArticle->A_NR . " ean: " . (string)$oArticle->A_EAN . PHP_EOL;
}
}
} else {
print_r($sResponse);
}
72 changes: 72 additions & 0 deletions php/getOrderMessages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

/**
* This example script gets information on changed order-status from the Tradebyte REST API.
* Then it marks the messages as processed.
*
* @author Marcos Doellerer <marcos.doellerer@fatchip.de>
*/

/*
* API-Credentials - modify to your personal access credentials
*
* @see https://tradebyte.io/how-to/generate-rest-api-credentials-in-tb-one/
*/
$sApiUser = 'api-user'; // Your API username
$sApiPassword = 'api-password'; // Your API password
$sMerchantId = '1234'; // Your digit merchant ID
$sChannelId = '5678'; // Your digit channel ID


/*
* Get data from REST API with curl
*/
$sUrl = "https://rest.trade-server.net/" . $sMerchantId . "/messages/?channel=" . $sChannelId;

$oCurl = curl_init();
curl_setopt($oCurl, CURLOPT_URL, $sUrl);
curl_setopt($oCurl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($oCurl, CURLOPT_USERPWD, $sApiUser . ":" . $sApiPassword);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($oCurl, CURLOPT_HEADER, 0);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($oCurl, CURLOPT_TIMEOUT, 3600);
$sResponse = curl_exec($oCurl);
if ($sResponse === false) {
echo 'Error: ' . curl_error($oCurl) . ' ErrorNr: ' . curl_errno($oCurl);
}
curl_close($oCurl);

/**
* Process data as you need
*/
$oXml = simplexml_load_string($sResponse);
if ($oXml) {
foreach ($oXml->MESSAGE as $oMessage) {
echo "Message Type: " . (string)$oMessage->MESSAGE_TYPE . PHP_EOL;
echo " Order number in Channel : " . (string)$oMessage->CHANNEL_ORDER_ID . PHP_EOL;

/**
* Sending confirmation to Tradebyte
*/
$sUrl = "https://rest.trade-server.net/" . $sMerchantId . "/messages/" . (int)$oMessage->MESSAGE_ID . "/processed?channel=" . $sChannelId;
$oCurl = curl_init();
curl_setopt($oCurl, CURLOPT_URL, $sUrl);
curl_setopt($oCurl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($oCurl, CURLOPT_USERPWD, $sApiUser . ":" . $sApiPassword);
curl_setopt($oCurl, CURLOPT_POST, true); // This is a POST request!
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($oCurl, CURLOPT_HEADER, 0);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($oCurl, CURLOPT_TIMEOUT, 30);
$sResponse = curl_exec($oCurl);
if ($sResponse === false) {
echo 'Error: ' . curl_error($oCurl) . ' ErrorNr: ' . curl_errno($oCurl);
} else {
echo "'Message received' confirmation for Message id: " . $oMessage->MESSAGE_ID . " successfully sent." . PHP_EOL;
}
curl_close($oCurl);
}
} else {
print_r($sResponse);
}
105 changes: 105 additions & 0 deletions php/getOrders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

/**
* This example script gets information on orders not yet exported from the Tradebyte REST API.
* Then marks them as exported
*
* @author Marcos Doellerer<marcos.doellerer@fatchip.de>
*/

/*
* API-Credentials - modify to your personal access credentials
*
* @see https://tradebyte.io/how-to/generate-rest-api-credentials-in-tb-one/
*/
$sApiUser = 'api-user'; // Your API username
$sApiPassword = 'api-password'; // Your API password
$sMerchantId = '1234'; // Your digit merchant ID
$sChannelId = '5678'; // Your digit channel ID

/*
* Get data from REST API with curl
*/
$sUrl = "https://rest.trade-server.net/" . $sMerchantId . "/orders/?channel=" . $sChannelId;
$oCurl = curl_init();
curl_setopt($oCurl, CURLOPT_URL, $sUrl);
curl_setopt($oCurl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($oCurl, CURLOPT_USERPWD, $sApiUser . ":" . $sApiPassword);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($oCurl, CURLOPT_HEADER, 0);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($oCurl, CURLOPT_TIMEOUT, 3600);
$sResponse = curl_exec($oCurl);
if ($sResponse === false) {
echo 'Error: ' . curl_error($oCurl) . ' ErrorNr: ' . curl_errno($oCurl);
}
curl_close($oCurl);

/**
* Process data as you need.
*/
$oXml = simplexml_load_string($sResponse);

if ($oXml) {
$ordersImported = [];

foreach ($oXml->ORDER as $oOrder) {
echo "Order Date " . (string)$oOrder->ORDER_DATA->ORDER_DATE . PHP_EOL;

$orderId = (string)$oOrder->ORDER_DATA->TB_ID;
echo "Order ID in Tradebyte " . $orderId . PHP_EOL;
echo "Order Number (in channel): " . (string)$oOrder->ORDER_DATA->CHANNEL_NO . PHP_EOL;

/**
* Example of Shipping Information
*/
if ($oOrder->SELL_TO) {
echo "Payment Address: " . $oXml->ORDER->SELL_TO->STREET_NO . PHP_EOL;
}
/**
* Tag SHIP_TO is mandatory so we don't need to check existence
*/
echo "Shipping Address: " . $oXml->ORDER->SHIP_TO->STREET_NO . PHP_EOL;

echo "Order Items: " . PHP_EOL;
/**
* Here we loop through Order's Items
*/
foreach ($oOrder->ITEMS->ITEM as $oOrderItem) {
echo "Article EAN " . (string)$oOrderItem->EAN .
" Quantity: " . (string)$oOrderItem->QUANTITY .
" Price: " . (string)$oOrderItem->ITEM_PRICE . PHP_EOL;
}
/**
* Sending confirmation to Tradebyte
*/
sendExportedFlagToTradebyte($orderId, $sMerchantId, $sApiUser, $sApiPassword);
}
} else {
print_r($sResponse);
}



function sendExportedFlagToTradebyte($orderId, $sMerchantId, $sApiUser, $sApiPassword)
{

$sUrl = "https://rest.trade-server.net/" . $sMerchantId . "/orders/" . $orderId . "/exported?";

$oCurl = curl_init();
curl_setopt($oCurl, CURLOPT_URL, $sUrl);
curl_setopt($oCurl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($oCurl, CURLOPT_USERPWD, $sApiUser . ":" . $sApiPassword);
curl_setopt($oCurl, CURLOPT_POST, true); // This is a POST request!
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($oCurl, CURLOPT_HEADER, 0);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($oCurl, CURLOPT_TIMEOUT, 30);
$sResponse = curl_exec($oCurl);
if ($sResponse === false) {
echo 'Error: ' . curl_error($oCurl) . ' ErrorNr: ' . curl_errno($oCurl);
} else {
echo "Export Confirmation for Order: " . $orderId . " successfully sent." . PHP_EOL;
}
curl_close($oCurl);
}
47 changes: 47 additions & 0 deletions php/getStocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/**
* This example script gets stocks for all articles from the Tradebyte REST API.
*
* @author Hendrik Bahr <bahr@fatchip.de>
*/

/*
* API-Credentials - modify to your personal access credentials
*
* @see https://tradebyte.io/how-to/generate-rest-api-credentials-in-tb-one/
*/
$sApiUser = 'api-user'; // Your API username
$sApiPassword = 'api-password'; // Your API password
$sMerchantId = '1234'; // Your digit merchant ID
$sChannelId = '5678'; // Your digit channel ID

/*
* Get data from REST API with curl
*/
$sUrl = "https://rest.trade-server.net/" . $sMerchantId . "/stock/?channel=" . $sChannelId;
$oCurl = curl_init();
curl_setopt($oCurl, CURLOPT_URL, $sUrl);
curl_setopt($oCurl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($oCurl, CURLOPT_USERPWD, $sApiUser . ":" . $sApiPassword);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($oCurl, CURLOPT_HEADER, 0);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($oCurl, CURLOPT_TIMEOUT, 3600);
$sResponse = curl_exec($oCurl);
if ($sResponse === false) {
echo 'Error: ' . curl_error($oCurl) . ' ErrorNr: ' . curl_errno($oCurl);
}
curl_close($oCurl);

/*
* Process data as you need
*/
$oXml = simplexml_load_string($sResponse);
if ($oXml && $oXml->ARTICLE) {
foreach ($oXml->ARTICLE as $oProduct) {
echo (string)$oProduct->A_NR . ": " . (string)$oProduct->A_STOCK . PHP_EOL;
}
} else {
print_r($sResponse);
}
Loading