Skip to content

Instantly share code, notes, and snippets.

@panoply
Created July 26, 2016 08:43
Show Gist options
  • Select an option

  • Save panoply/321bb34dd7af82f271925bfd33bc7ae6 to your computer and use it in GitHub Desktop.

Select an option

Save panoply/321bb34dd7af82f271925bfd33bc7ae6 to your computer and use it in GitHub Desktop.
Silk Stock POST request (EXAMPLE) using Guzzle 6.0 >

Setup

Dependency Inversion principle design pattern (which is where you find SILK API connection). Here is an example of how I am talking to the SILK API. The GET request as of 2 days ago is now returning an empty page, this also tested using POSTMAN. POST requests using Guzzle continually fail, have never worked.

Build with Laravel 5.2 framework.

.ENV Values (Secret is valid):

SILK_DOMAIN=https://ss.orderdogs.com/plugin-export/seven-sunday/ SILK_SECRET=xxxxxxxxxxxxxxxxxxxxxxxx

Route::post('/update/silk', 'SilkController@update');
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Repositories\SilkInterface;
use App\Http\Repositories\ShopifyInterface;
use App\Stock;
use App\Http\Requests;
use DB;
use Shopify;
class SilkController extends Controller {
private $SilkApi;
private $ShopifyApi;
public function __construct(SilkInterface $SilkApi, ShopifyInterface $ShopifyApi) {
$this->SilkApi = $SilkApi;
$this->ShopifyApi = $ShopifyApi;
}
/**
* Sync Stock from Silk
* Push quantity levels from Silk to Database.
*
* Chron Task (Running every 30 minutes)
*
*/
public function SyncSilk(Request $request) {
try {
$silk = $this->SilkApi->get('stock');
$data = array();
foreach ($silk as $item) {
$data[] = array(
'product' => $item->product .'–'. $item->variant,
'size' => $item->size,
'weight' => $item->weight,
'product_sku' => $item->sku,
'variant_sku' => $item->variantSku,
'ean' => $item->ean,
'available_stock' => $item->availableStock,
'allocated_stock' => $item->allocatedStock,
'physical_stock' => $item->physicalStock
);
}
try {
Stock::insert($data);
} catch (Exception $e) {
Log::info('Silk API Stock Sync Failed');
}
}
catch (Exception $e)
{
Log::info('Silk API Stock Sync Failed');
}
}
/**
* FAILING :: Update Stock in Silk.
*
*
*/
public function update(Request $request) {
$data = ['products' =>
['product' => '07332927917363', 'quantity' => 200], // has valid EAN code
['product' => '07332927917370', 'quantity' => 200]
];
return $this->SilkApi->update($data, 'stock');
}
}
<?php
namespace App\Http\Repositories;
use GuzzleHttp\Psr7\Request;
/**
* API Connections
*
* Calls API connections for SILK and
* Shopify in Controllers.
*
*/
interface SilkInterface {
/**
* Silk Api Connection
*
* @return connection
*/
public function get($resource);
public function update($data, $resource);
}
<?php
namespace App\Http\Repositories;
use App\Http\Repositories\SilkInterface;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Request;
use Guzzle\Http\EntityBody;
class SilkRepository implements SilkInterface {
/**
* GET Stock and Variant via SILKs API.
*
*
*/
public function get($resource) {
$client = new Client();
$response = $client->get(env('SILK_DOMAIN'). $resource,
[ 'query' =>
[ 'secret' => env('SILK_SECRET'),
'created' => '2016-01-01']]
);
$json = json_decode($response->getBody());
return $json->products;
}
/**
* FAILING :: Update Stock in Silk.
*
*
*/
public function update($data, $resource) {
$client = new Client();
$response = $client->request('POST',env('SILK_DOMAIN'). $resource, [
'debug' => TRUE,
'form_params' => $data,
'headers' => [
'Content-Type' => 'application/json',
'API-authorization' => env('SILK_SECRET')
]]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment