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 <[email protected]>
*/

/*
* 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 <[email protected]>
*/

/*
* 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);
}
71 changes: 71 additions & 0 deletions php/getOrderMessages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?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 <[email protected]>
*/

/*
* 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/" . (string)$oMessage->TB_ORDER_ID . "/processed?channel=" . $sChannelId;
mdoellerer-fc marked this conversation as resolved.
Show resolved Hide resolved
$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: " . $messageId . " successfully sent." . PHP_EOL;
mdoellerer-fc marked this conversation as resolved.
Show resolved Hide resolved
}
curl_close($oCurl);
}
} else {
print_r($sResponse);
}
110 changes: 110 additions & 0 deletions php/getOrders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
/**
* This example script gets information on orders not yet exported from the Tradebyte REST API.
* Then marks them as exported
*
* @author Marcos Doellerer<[email protected]>
*/

/*
* 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;

$ordersImported[] = (string)$oOrder->ORDER_DATA->TB_ID;
echo "Order ID in Tradebyte " . (string)$oOrder->ORDER_DATA->TB_ID . 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($ordersImported, $sMerchantId, $sApiUser, $sApiPassword);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on the messages example we do the confirmation at the end inside of the loop and not collecting the message identifiers. i would suggest the same process here.


} else {
print_r($sResponse);
}



function sendExportedFlagToTradebyte($arrayOfIds, $sMerchantId, $sApiUser, $sApiPassword){
mdoellerer-fc marked this conversation as resolved.
Show resolved Hide resolved

foreach ($arrayOfIds as $orderId){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment above, remove loop here

$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);
}
}
46 changes: 46 additions & 0 deletions php/getStocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* This example script gets stocks for all articles from the Tradebyte REST API.
*
* @author Hendrik Bahr <[email protected]>
*/

/*
* 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