Skip to content

Commit 5580815

Browse files
committed
More work on readme
1 parent 513cdb9 commit 5580815

File tree

1 file changed

+117
-3
lines changed

1 file changed

+117
-3
lines changed

readme.md

Lines changed: 117 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Add `"rocket-code/shopify": "~1.0"` in your "require" object. With a blank Larav
1515
}
1616
```
1717
####Add the Service Provider
18-
In `app/config/app.php`, add `'RocketCode\Shopify\ShopifyServiceProvider'` to the end of the `'providers'` array.
18+
In `app/config/app.php`, add `RocketCode\Shopify\ShopifyServiceProvider` to the end of the `providers` array.
1919

2020
##Setting Up
2121
To begin, use `App::make()` to grab an instance of the `API` class.
@@ -60,7 +60,9 @@ Once the user accesses the Install URL and clicks the Install button, they will
6060
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:
6161

6262
```
63+
$code = Input::get('code');
6364
$sh = App::make('ShopifyAPI', ['API_KEY' => '', 'API_SECRET' => '', 'SHOP_DOMAIN' => '']);
65+
6466
try
6567
{
6668
$accessToken = $sh->getAccessToken($code);
@@ -82,7 +84,8 @@ try
8284
$verify = $sh->verifyRequest(Input::all());
8385
if ($verify)
8486
{
85-
// Make getAccessToken() request here
87+
$code = Input::get('code');
88+
$accessToken = $sh->getAccessToken($code);
8689
}
8790
else
8891
{
@@ -99,5 +102,116 @@ catch (Exception $e)
99102
100103
```
101104

102-
`verifyRequest()` 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.
105+
`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.
106+
107+
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:
108+
109+
```
110+
$verify = $sh->verifyRequest(Input::all(), TRUE);
111+
```
112+
113+
## Private Apps
114+
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.
115+
116+
##Calling the API
117+
Once set up, simply pass the data you need to the `call()` method.
118+
119+
```
120+
$result = $sh->call($args);
121+
```
122+
123+
####`call()` Parameters
124+
The parameters listed below allow you to set required values for an API call as well as override additional default values.
125+
126+
* `METHOD`: The HTTP method to use for your API call. Different endpoints require different methods.
127+
* Default: `GET`
128+
* `URL`: The URL of the API Endpoint to call.
129+
* Default: `/` (not an actual endpoint)
130+
* `HEADERS`: An array of additional Headers to be sent
131+
* Default: Empty `array()`. Headers that are automatically sent include:
132+
* Accept
133+
* Content-Type
134+
* charset
135+
* X-Shopify-Access-Token
136+
* `CHARSET`: Change the charset if necessary
137+
* Default: `UTF-8`
138+
* `DATA`: An array of data being sent with the call. For example, `$args['DATA'] = array('product' => $product);` For an [`/admin/products.json`](http://docs.shopify.com/api/product#create) product creation `POST`.
139+
* Default: Empty `array()`
140+
* `RETURNARRAY`: Set this to `TRUE` to return data in `array()` format. `FALSE` will return a `stdClass` object.
141+
* Default: `FALSE`
142+
* `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.
143+
* Default: `FALSE`
144+
* `FAILONERROR`: The value passed to cURL's [CURLOPT_FAILONERROR](http://php.net/manual/en/function.curl-setopt.php) 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.
145+
* Default: `TRUE`
146+
147+
148+
##Some Examples
149+
Assume that `$sh` has already been set up as documented above.
150+
151+
####Listing Products
152+
```
153+
try
154+
{
155+
156+
$call = $sh->call(['URL' => 'products.json', 'METHOD' => 'GET', 'DATA' => ['limit' => 5, 'published_status' => 'any']]);
157+
}
158+
catch (Exception $e)
159+
{
160+
$call = $e->getMessage();
161+
}
162+
163+
echo '<pre>';
164+
var_dump($call);
165+
echo '</pre>';
166+
167+
```
168+
169+
`$call` will either contain a `stdClass` object with `products` or an Exception error message.
170+
171+
####Creating a snippet from a Laravel View
172+
173+
```
174+
$testData = ['name' => 'Foo', 'location' => 'Bar'];
175+
$view = (string) View::make('snippet', $testData);
176+
177+
$themeID = 12345678;
178+
179+
try
180+
{
181+
$call = $sh->call(['URL' => '/admin/themes/' . $themeID . '/assets.json', 'METHOD' => 'PUT', 'DATA' => ['asset' => ['key' => 'snippets/test.liquid', 'value' => $view] ] ]);
182+
}
183+
catch (Exception $e)
184+
{
185+
$call = $e->getMessage();
186+
}
187+
188+
echo '<pre>';
189+
var_dump($call);
190+
echo '</pre>';
191+
```
192+
193+
####Performing operations on multiple shops
194+
The `setup()` method makes changing the current shop simple.
195+
196+
```
197+
$apiKey = '123';
198+
$apiSecret = '456';
199+
200+
$sh = App::make('ShopifyAPI', ['API_KEY' => $apiKey, 'API_SECRET' => $apiSecret]);
201+
202+
$shops = array(
203+
'my-shop.myshopify.com' => 'abc',
204+
'your-shop.myshopify.com' => 'def',
205+
'another.myshopify.com' => 'ghi'
206+
);
207+
208+
foreach($shops as $domain => $access)
209+
{
210+
$sh->setup(['SHOP_DOMAIN' => $domain, 'ACCESS_TOKEN'' => $access]);
211+
// $sh->call(), etc
212+
213+
}
214+
215+
```
216+
103217

0 commit comments

Comments
 (0)