dev-master
9999999-devAPI Wrapper to connect to Microsoft Partner Center.
The Requires
by Digvijay Singh
1.0.0
1.0.0.0API Wrapper to connect to Microsoft Partner Center.
The Requires
by Digvijay Singh
API Wrapper to connect to Microsoft Partner Center.
An easy-to-use PHP package to communicate with Microsoft's API in Laravel., (*1)
composer.json
Add "tech-sprinters/partnercenter"
in your "require" object. With a blank Laravel install, it will look something like this:, (*2)
For Laravel 5, use "tech-sprinters/partnercenter": "~2.0"
., (*3)
In app/config/app.php
, add PartnerCenter\Microsoft\MicrosoftServiceProvider
to the end of the providers
array., (*4)
To begin, use App::make()
to grab an instance of the API
class., (*5)
$ms = App::make('Microsoft');
Simply pass an array with the following keys (and filled-in values) to prepare. Not all values need to be passed at once; you can call the setup()
method as many times as you'd like; it will only accept the following 4 keys, and overwrite a values if it's already set., (*6)
$ms->setup(['API_KEY' => '', 'API_SECRET' => '', 'SHOP_DOMAIN' => '', 'ACCESS_TOKEN' => '']);
Pass the setup array as the second argument in App::make()
:, (*7)
$sh = App::make('ShopifyAPI', ['API_KEY' => '', 'API_SECRET' => '', 'SHOP_DOMAIN' => '', 'ACCESS_TOKEN' => '']);
That's it! You're ready to make some API calls., (*8)
After setting up with at least SHOP_DOMAIN
& API_KEY
, call installURL()
with an array of permissions (the app's Scope):, (*9)
$sh->installURL(['permissions' => array('write_orders', 'write_products')]);
You may also pass a redirect URL per the redirect_uri
parameter as described by the Shopify API Docs, (*10)
$sh->installURL(['permissions' => array('write_orders', 'write_products'), 'redirect' => 'http://myapp.com/success']);
In order to make Authenticated requests, the Access Token must be passed as a header in each request. This package will automatically do that for you, but you must first authenticate your app on each store (as the user installs it), and save the Access Token., (*11)
Once the user accesses the Install URL and clicks the Install button, they will be redirected back to your app with data in the Query String., (*12)
After setting up with at least SHOP_DOMAIN
, API_KEY
, & API_SECRET
, call getAccessToken()
with the code passed back in the URL. Laravel makes this easy:, (*13)
$code = Input::get('code'); $sh = App::make('ShopifyAPI', ['API_KEY' => '', 'API_SECRET' => '', 'SHOP_DOMAIN' => '']); try { $accessToken = $sh->getAccessToken($code); } catch (Exception $e) { echo 'Error: ' . $e->getMessage() . ''; } // Save $accessToken
Shopify returns a hashed value to validate the data against. To validate (recommended before calling getAccessToken()
), utilize verifyRequest()
., (*14)
try { $verify = $sh->verifyRequest(Input::all()); if ($verify) { $code = Input::get('code'); $accessToken = $sh->getAccessToken($code); } else { // Issue with data } } catch (Exception $e) { echo 'Error: ' . $e->getMessage() . ''; }
verifyRequest()
returns TRUE
when data is valid, otherwise FALSE
. It throws an Exception in two cases: If the timestamp generated by Shopify and your server are more than an hour apart, or if the argument passed is not an array or URL-encoded string of key/values., (*15)
If you would like to skip the timestamp check (not recommended unless you cannot correct your server's time), you can pass TRUE
as a second argument to verifyRequest()
and timestamps will be ignored:, (*16)
$verify = $sh->verifyRequest(Input::all(), TRUE);
The API Wrapper does not distinguish between private and public apps. In order to utilize it with a private app, set up everything as you normally would, replacing the OAuth Access Token with the private app's Password., (*17)
Once set up, simply pass the data you need to the call()
method., (*18)
$result = $sh->call($args);
call()
ParametersThe parameters listed below allow you to set required values for an API call as well as override additional default values., (*19)
METHOD
: The HTTP method to use for your API call. Different endpoints require different methods.
GET
URL
: The URL of the API Endpoint to call.
/
(not an actual endpoint)HEADERS
: An array of additional Headers to be sent
array()
. Headers that are automatically sent include:CHARSET
: Change the charset if necessary
UTF-8
DATA
: An array of data being sent with the call. For example, $args['DATA'] = array('product' => $product);
For an /admin/products.json
product creation POST
.
array()
RETURNARRAY
: Set this to TRUE
to return data in array()
format. FALSE
will return a stdClass
object.
FALSE
ALLDATA
: Set this to TRUE
if you would like all error and cURL info returned along with your API data (good for debugging). Data will be available in $result->_ERROR
and $result->_INFO
, or $result['_ERROR']
and $result['_INFO']
, depending if you are having it returned as an object or array. Recommended to be set to FALSE
in production.
FALSE
FAILONERROR
: The value passed to cURL's CURLOPT_FAILONERROR setting. TRUE
will cause the API Wrapper to throw an Exception if the HTTP code is >= 400
. FALSE
in combination with ALLDATA
set to TRUE
will give you more debug information.
TRUE
Assume that $sh
has already been set up as documented above., (*20)
try { $call = $sh->call(['URL' => 'products.json', 'METHOD' => 'GET', 'DATA' => ['limit' => 5, 'published_status' => 'any']]); } catch (Exception $e) { $call = $e->getMessage(); } echo ''; var_dump($call); echo '';
$call
will either contain a stdClass
object with products
or an Exception error message., (*21)
$testData = ['name' => 'Foo', 'location' => 'Bar']; $view = (string) View::make('snippet', $testData); $themeID = 12345678; try { $call = $sh->call(['URL' => '/admin/themes/' . $themeID . '/assets.json', 'METHOD' => 'PUT', 'DATA' => ['asset' => ['key' => 'snippets/test.liquid', 'value' => $view] ] ]); } catch (Exception $e) { $call = $e->getMessage(); } echo ''; var_dump($call); echo '';
The setup()
method makes changing the current shop simple., (*22)
$apiKey = '123'; $apiSecret = '456'; $sh = App::make('ShopifyAPI', ['API_KEY' => $apiKey, 'API_SECRET' => $apiSecret]); $shops = array( 'my-shop.myshopify.com' => 'abc', 'your-shop.myshopify.com' => 'def', 'another.myshopify.com' => 'ghi' ); foreach($shops as $domain => $access) { $sh->setup(['SHOP_DOMAIN' => $domain, 'ACCESS_TOKEN'' => $access]); // $sh->call(), etc }
API Wrapper to connect to Microsoft Partner Center.
API Wrapper to connect to Microsoft Partner Center.