Examples¶
Configuration¶
return array(
/*
* Application ID generated by AppStore
*/
'appId' => '',
/*
* Secret generated by AppStore
*/
'appSecret' => '',
/*
* AppStore Secret generated by AppStore
*/
'appstoreSecret' => '',
/*
* Enable debug mode or not
*/
'debug' => false,
/*
* Path to log file or empty to disable logging
*/
'logFile' => "logs/application.log",
/*
* timezone of the application
*
* Value is passed to date_default_timezone_set function
*/
'timezone' => 'Europe/Warsaw',
'php' => array(
/*
* This determines whether errors should be printed to the screen as
* part of the output or if they should be hidden from the user
*
* Value is passed to ini_set function
*/
'display_errors' => 'off'
)
);
Bootstrapping¶
// force utf-8 as primary encoding
if (PHP_VERSION_ID < 50600) {
mb_internal_encoding('utf-8');
} else {
ini_set('default_charset', 'utf-8');
}
// internal autoloader
spl_autoload_register(function($class){
$class = str_replace('\\', '/', $class);
require 'src/'.$class.'.php';
});
// composer autoloader - you can enable it on by uncommenting this line:
//require 'vendor/autoload.php';
// use config from previous example
$config = require __DIR__. '/Config.php';
// various PHP configuration values
date_default_timezone_set($config['timezone']);
ini_set('display_errors', $config['php']['display_errors']);
// check debug mode options
$debug = false;
if(isset($config['debug'])){
if($config['debug']){
$debug = true;
}
}
// check environment variable
if(getenv('DREAMCOMMERCE_DEBUG')){
$debug = true;
}
define("DREAMCOMMERCE_DEBUG", $debug);
// log errors to stdout by default
$logFile = "php://stdout";
if(isset($config['logFile'])){
if($config['logFile']){
$logFile = $config['logFile'];
}else{
$config['logFile'] = false;
}
}
define("DREAMCOMMERCE_LOG_FILE", $logFile);
return $config;
REST GET¶
use DreamCommerce\ShopAppstoreLib\Client;
use DreamCommerce\ShopAppstoreLib\Exception\ClientException;
use DreamCommerce\ShopAppstoreLib\Exception\ResourceException;
$config = require 'bootstrap.php';
try {
// set custom retries count
// it will throw HttpException if the limit is too low
\DreamCommerce\ShopAppstoreLib\Http::setRetryLimit(2);
$client = Client::factory(
Client::ADAPTER_OAUTH,
array(
'entrypoint'=>'https://myshop.example.com',
'client_id'=>$config['appId'],
'client_secret'=>$config['appSecret']
)
);
$client->setAccessToken('INSERT TOKEN HERE');
$resource = new \DreamCommerce\ShopAppstoreLib\Resource\Product($client);
// or
$resource = $client->products;
// particular object, with ID=1
$result = $resource->get(1);
// list of objects
$result = $resource->get();
// list of objects (page 3) with filtering/limiting:
$result = $resource->filters(array('translations.name' => array('=', 'laptop')))->page(3)->limit(10)->get();
printf("Found: %d\n", $result->count);
printf("Page: %d of %d\n", $result->page, $result->pages);
printf("Iterating over products:\n");
foreach ($result as $i) {
printf("ID #%d\n", $i->product_id);
// or - for your convenience:
//printf("ID #%d\n", $i['product_id']);
}
} catch (ClientException $ex) {
$client->getLogger()->error("An error occurred during the Client initialization: ".Client::getError($ex));
} catch (ResourceException $ex) {
$client->getLogger()->error("An error occurred during Resource access: ".Client::getError($ex));
}
REST POST¶
use DreamCommerce\ShopAppstoreLib\Client;
use DreamCommerce\ShopAppstoreLib\Exception\ClientException;
use DreamCommerce\ShopAppstoreLib\Exception\ResourceException;
$config = require 'bootstrap.php';
try {
// set custom retries count
// it will throw HttpException if the limit is too low
\DreamCommerce\ShopAppstoreLib\Http::setRetryLimit(2);
$client = Client::factory(
Client::ADAPTER_OAUTH,
array(
'entrypoint'=>'https://myshop.example.com',
'client_id'=>$config['appId'],
'client_secret'=>$config['appSecret']
)
);
$client->setAccessToken('INSERT TOKEN HERE');
$resource = new \DreamCommerce\ShopAppstoreLib\Resource\Producer($client);
// or
$resource = $client->producers;
$insertedId = $resource->post(array(
'name' => 'Awesome Manufacturer!',
'web' => 'http://example.org'
));
// or:
$data = new stdClass();
$data->name = 'Awesome Manufacturer!';
$data->web = 'http://example.org';
$insertedId = $resource->post($data);
} catch (ClientException $ex) {
$client->getLogger()->error("An error occurred during the Client initialization: ".Client::getError($ex));
} catch (ResourceException $ex) {
$client->getLogger()->error("An error occurred during Resource access: ".Client::getError($ex));
}
REST PUT¶
use DreamCommerce\ShopAppstoreLib\Client;
use DreamCommerce\ShopAppstoreLib\Exception\ClientException;
use DreamCommerce\ShopAppstoreLib\Exception\ResourceException;
$config = require 'bootstrap.php';
try {
// set custom retries count
// it will throw HttpException if the limit is too low
\DreamCommerce\ShopAppstoreLib\Http::setRetryLimit(2);
$client = Client::factory(
Client::ADAPTER_OAUTH,
array(
'entrypoint'=>'https://myshop.example.com',
'client_id'=>$config['appId'],
'client_secret'=>$config['appSecret']
)
);
$client->setAccessToken('INSERT TOKEN HERE');
$resource = new \DreamCommerce\ShopAppstoreLib\Resource\Producer($client);
// or
$resource = $client->producers;
$insertedId = $resource->put(2, array(
'name' => 'Awesome Manufacturer!'
));
$client->getLogger()->info("Object modified");
} catch (ClientException $ex) {
$client->getLogger()->error("An error occurred during the Client initialization: ".Client::getError($ex));
} catch (ResourceException $ex) {
$client->getLogger()->error("An error occurred during Resource access: ".Client::getError($ex));
}
REST DELETE¶
use DreamCommerce\ShopAppstoreLib\Client;
use DreamCommerce\ShopAppstoreLib\Exception\ClientException;
use DreamCommerce\ShopAppstoreLib\Exception\ResourceException;
$config = require 'bootstrap.php';
try {
// set custom retries count
// it will throw HttpException if the limit is too low
\DreamCommerce\ShopAppstoreLib\Http::setRetryLimit(2);
$client = Client::factory(
Client::ADAPTER_OAUTH,
array(
'entrypoint'=>'https://myshop.example.com',
'client_id'=>$config['appId'],
'client_secret'=>$config['appSecret']
)
);
$client->setAccessToken('INSERT TOKEN HERE');
$resource = new \DreamCommerce\ShopAppstoreLib\Resource\Producer($client);
// or
$resource = $client->producers;
$result = $resource->delete(41);
$client->getLogger()->info("An object was successfully deleted");
} catch (ClientException $ex) {
$client->getLogger()->error("An error occurred during the Client initialization: ".Client::getError($ex));
} catch (ResourceException $ex) {
$client->getLogger()->error("An error occurred during Resource access: ".Client::getError($ex));
}
Token refreshing¶
use DreamCommerce\ShopAppstoreLib\Client;
use DreamCommerce\ShopAppstoreLib\Exception\ClientException;
$config = require 'bootstrap.php';
try {
$client = Client::factory(
Client::ADAPTER_OAUTH,
array(
'entrypoint'=>'https://myshop.example.com',
'client_id'=>$config['appId'],
'client_secret'=>$config['appSecret']
]
);
$client->setAccessToken('INSERT TOKEN HERE');
$client->getLogger()->info("Token has been successfully refreshed");
} catch (ClientException $ex) {
$client->getLogger()->error("An error occurred during the Client request: ".$ex->getMessage());
}