Continut articol
Articolul prezinta un exemplu de cod PHP util pentru integrarea cu Serverul REST SmartCash Everywhere. Codul este bazat pe bibliotecile PHP CURL.
/*
* With credits to Mr. Vlad Banyai from ContentSpeed Bucharest
* for this excelent piece of code for SmartCash Everywhere REST
* integration using PHP – CURL Library 2014.05.29
*
* The code is provided for JSON – Format 2 response but it can be adapted also for
* pair fields Format 1 JSON returned by SmartCash Everywhere REST Server
*/
<?php
class SMARTCASHREST {
//replace with your own REST Server URL
CONST URL = ‘http://everywhere.smartcash.ro/everywhere/rest/TSmartCashMethods/’;
//replace with your APP code
CONST IDAPP = ‘1’;
//shop id; replace with correct shop id
CONST NRSHOP = ’99’;
/**
* Get total stock for modified articles
* from SmartCash RMS and confirm received data
*/
function updateStock(){
$url = self::URL . ‘GetModifiedStockTotal’ . ‘/’ . self::IDAPP;
$user_agent = “Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)”;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
if($response->result[0]->DATASET[1]){
//only for this example, but here you can process here the JSON response and validate the answer
foreach($response->result[0]->DATASET as $key=>$res){
print_r($res);
if($key>0){
print_r($res);
//cod client
}
}
//confirm received data
$confirmUrl = $response->result[0]->DATASET[1]->REC1[0];
$confirmUrl = self::URL . ‘ConfirmReceivingDataByTypeOf’ . ‘/’ . self::IDAPP . ‘/1/’ . self::NRSHOP . ‘/’ . $confirmUrl;
$user_agent = “Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)”;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $confirmUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
}
}
/**
* Get modified articles from SmartCash RMS
* and confirm received data
*/
function updatePrice(){
$url = self::URL . ‘GetModifiedArticles’ . ‘/’ . self::IDAPP . ‘/’ . self::NRSHOP ;
$user_agent = “Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)”;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
//only for this example, but here you can process here the JSON response and validate the answer
if($response->result[0]->DATASET[1]){
foreach($response->result[0]->DATASET as $key=>$res){
if($key>0){
print_r($res);
//cod client
}
}
//confirm received data
$confirmUrl = $response->result[0]->DATASET[1]->REC1[0];
$confirmUrl = self::URL . ‘ConfirmReceivingDataByTypeOf’ . ‘/’ . self::IDAPP . ‘/101/’ . self::NRSHOP . ‘/’ . $confirmUrl;
$user_agent = “Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)”;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $confirmUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
}
}
/**
* Get all articles from SmartCash RMS for a certain external code
*
* @param string $productCode external code from SmartCash for which to return al coresponding articles
*
*/
function updateProduct($productCode){
$url = self::URL . ‘GetArticleInfo’ . ‘/’ . self::IDAPP . ‘/2/’ . self::NRSHOP . ‘/’ . $productCode ;
$user_agent = “Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)”;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
if($response->result[0]->DATASET[1]){
//only for this example, but here you can process here the JSON response and validate the answer
foreach($response->result[0]->DATASET as $key=>$res){
if($key>0){
print_r($res);
//cod client
}
}
}
}
/**
* Get modified customers from SmartCash RMS
* and confirm received data
*/
function updateCustomers(){
$url = self::URL . ‘GetModifiedCustomers’ . ‘/’ . self::IDAPP;
$user_agent = “Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)”;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
if($response->result[0]->DATASET[1]){
//only for this example, but here you can process here the JSON response and validate the answer
foreach($response->result[0]->DATASET as $key=>$res){
if($key>0){
print_r($res);
//cod client
}
}
//confirm received data
$confirmUrl = $response->result[0]->DATASET[1]->REC1[0];
$confirmUrl = self::URL . ‘ConfirmReceivingDataByTypeOf’ . ‘/’ . self::IDAPP . ‘/201/’ . self::NRSHOP . ‘/’ . $confirmUrl;
$user_agent = “Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)”;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $confirmUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
}
}
/**
* Add a new customer to SmartCash RMS
*
* @param array $customer with all customer data
*
*/
function addCustomer($customer){
$request = json_encode($customer);
if($request){
$url = self::URL . ‘”SaveCustomer”/’ . self::IDAPP;
}
$user_agent = “Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)”;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
print_r($response);
if($response->result[0]){
// cod client
}
}
}
//And here is the way ou can use the functions
$clsScRMS = new SMARTCASHREST();
//update product
//get articles by external code; replace with correct code
$clsScRMS->updateProduct(‘article code’);
//get modified customers
$clsScRMS->updateCustomers();
//get modified articles
$clsScRMS->updatePrice();
//get modified stock
$clsScRMS->updateStock();
//add customer
$customer[‘IDEXTAPP’] = 1;
$customer[‘NAME’] = ‘Test Test’;
$customer[‘TOWN’] = ‘Bucuresti’;
$customer[‘PHONE’] = ‘0700000000’;
$customer[‘EMAIL’] = ‘test@test.ro’;
$customer[‘VAT_NUMBER’] = 1;
$customer[‘VAT_NUMBER_TYPE’] = 1;
$customer[‘ALLOW_INVOICES’] = 1;
$customer[‘ADDRESS’] = ‘Test’;
$customer[‘IDCUSTOMER_TYPE’] = 1;
$customer[‘LISTED’] = 1;
$clsScRMS->addCustomer($customer);
?>
Descarca codul complet.