@@ -17,78 +17,44 @@ The recommended way to install is [through composer](http://packagist.org).
17
17
18
18
## Usage
19
19
20
+ Add ` use OhMyBrew\BasicShopifyAPI; ` to your imports.
21
+
20
22
### Public API
21
23
22
- For OAuth applications. The shop domain, API key, API secret, and an access token are required. This assumes you properly have your app setup in the partner's dashboard with the correct keys and redirect URIs.
24
+ This assumes you properly have your app setup in the partner's dashboard with the correct keys and redirect URIs.
23
25
24
- #### Quick run-down
26
+ #### REST
25
27
26
- ##### REST Method
28
+ For REST calls, the shop domain and access token are required.
27
29
28
30
``` php
29
- use OhMyBrew\ShopifyAPI;
30
-
31
- $api = new RestAPI;
32
- $api->setApiKey('your key here');
33
- $api->setApiSecret('your secret here');
31
+ $api = new BasicShopifyAPI();
32
+ $api->setShop('your shop here');
33
+ $api->setAccessToken('your token here');
34
34
35
- $api->setShop('example.myshopify.com');
36
- $api->setAccessToken('a token here');
37
- // or
38
- $api->setSession('example.myshopify.com', 'a token here');
39
-
40
- /**
41
- * $request will return an object with keys of `response` for full Guzzle response
42
- * `body` with JSON-decoded result
43
- */
44
- $request = $api->request('GET', '/admin/shop.json');
45
- echo $request->response->getStatusCode();
46
- echo $request->body->shop->name;
35
+ // Now run your requests...
36
+ $api->rest(...);
47
37
```
48
38
49
- ##### GraphQL Method
39
+ #### GraphQL
50
40
51
- ``` php
52
- use OhMyBrew\ShopifyAPI;
41
+ For GraphQL calls, the shop domain and access token are required.
53
42
54
- $api = new GraphAPI;
55
- $api->setApiKey('your key here');
56
- $api->setApiSecret('your secret here');
43
+ ``` php
44
+ $api = new BasicShopifyAPI();
45
+ $api->setShop('your shop here');
46
+ $api->setAccessToken('your token here');
57
47
58
- $api->setShop('example.myshopify.com');
59
- $api->setAccessToken('a token here');
60
- // or
61
- $api->setSession('example.myshopify.com', 'a token here');
62
-
63
- /**
64
- * $request will return an object with keys of `response` for full Guzzle response
65
- * `body` with JSON-decoded result
66
- */
67
- $query =<<<QL
68
- {
69
- shop {
70
- products(first: 2) {
71
- edges {
72
- node {
73
- id
74
- handle
75
- }
76
- }
77
- }
78
- }
79
- }
80
- QL;
81
- $request = $api- >request($query);
82
- echo $request->response->getStatusCode();
83
- echo $request->body->shop->products->edges[0]->node->handle;
48
+ // Now run your requests...
49
+ $api->graph(...);
84
50
```
85
51
86
52
#### Getting access token
87
53
88
54
After obtaining the user's shop domain, to then direct them to the auth screen use ` getAuthUrl ` , as example (basic PHP):
89
55
90
56
``` php
91
- $api = new RestAPI; // or GraphAPI
57
+ $api = new BasicShopifyAPI();
92
58
$api->setShop($_SESSION['shop']);
93
59
$api->setApiKey(env('SHOPIFY_API_KEY'));
94
60
@@ -110,7 +76,7 @@ if (!$code) {
110
76
$api->setAccessToken($token);
111
77
112
78
// You can now make API calls as well once you've set the token to `setAccessToken`
113
- $request = $api->request ('GET', '/admin/shop.json');
79
+ $request = $api->rest ('GET', '/admin/shop.json'); // or GraphQL
114
80
}
115
81
```
116
82
@@ -125,65 +91,43 @@ $valid = $api->verifyRequest($_GET);
125
91
126
92
### Private API
127
93
128
- For private application calls. The shop domain, API key, and API password are required .
94
+ This assumes you properly have your app setup in the partner's dashboard with the correct keys and redirect URIs .
129
95
130
- #### Quick run-down
96
+ #### REST
131
97
132
- ##### REST Method
98
+ For REST calls, shop domain, API key, and API password are request
133
99
134
100
``` php
135
- $api = new RestAPI (true); // true sets it to private
101
+ $api = new BasicShopifyAPI (true); // true sets it to private
136
102
$api->setShop('example.myshopify.com');
137
103
$api->setApiKey('your key here');
138
104
$api->setApiPassword('your password here');
139
105
140
- /**
141
- * $request will return an object with keys of `response` for full Guzzle response
142
- * `body` with JSON-decoded result
143
- */
144
- $request = $api->request('GET', '/admin/shop.json');
145
- echo $request->response->getStatusCode();
146
- echo $request->body->shop->name;
106
+ // Now run your requests...
107
+ $api->rest(...);
147
108
```
148
109
149
- ##### GraphQL Method
110
+ #### GraphQL
111
+
112
+ For GraphQL calls, shop domain and API password are required.
150
113
151
114
``` php
152
- $api = new GraphAPI (true); // true sets it to private
115
+ $api = new BasicShopifyAPI (true); // true sets it to private
153
116
$api->setShop('example.myshopify.com');
154
- $api->setApiPassword('your password here'); // This is used as the access token for GraphQL
155
-
156
- /**
157
- * $request will return an object with keys of `response` for full Guzzle response
158
- * `body` with JSON-decoded result
159
- */
160
- $query =<<<QL
161
- {
162
- shop {
163
- products(first: 2) {
164
- edges {
165
- node {
166
- id
167
- handle
168
- }
169
- }
170
- }
171
- }
172
- }
173
- QL;
174
- $request = $api- >request($query);
175
- echo $request->response->getStatusCode();
176
- echo $request->body->shop->products->edges[0]->node->handle;
117
+ $api->setApiPassword('your password here');
118
+
119
+ // Now run your requests...
120
+ $api->graph(...);
177
121
```
178
122
179
123
### Making requests
180
124
181
- #### REST Method
125
+ #### REST
182
126
183
127
Requests are made using Guzzle.
184
128
185
129
``` php
186
- $api->request (string $type, string $path, array $params = null);
130
+ $api->rest (string $type, string $path, array $params = null);
187
131
```
188
132
189
133
+ ` type ` refers to GET, POST, PUT, DELETE, etc
@@ -195,12 +139,14 @@ The return value for the request will be an object containing:
195
139
+ ` response ` the full Guzzle response object
196
140
+ ` body ` the JSON decoded response body
197
141
198
- #### GraphQL Method
142
+ * Note* : ` request() ` will alias to ` rest() ` as well.
143
+
144
+ #### GraphQL
199
145
200
146
Requests are made using Guzzle.
201
147
202
148
``` php
203
- $api->request (string $query);
149
+ $api->graph (string $query);
204
150
```
205
151
206
152
+ ` query ` refers to the full GraphQL query
@@ -217,7 +163,7 @@ After each request is made, the API call limits are updated. To access them, sim
217
163
``` php
218
164
// Returns an array of left, made, and limit.
219
165
// Example: ['left' => 79, 'made' => 1, 'limit' => 80]
220
- $limits = $api->getApiCalls();
166
+ $limits = $api->getApiCalls('rest'); // or 'graph'
221
167
```
222
168
223
169
For GraphQL, additionally there will be the following values: ` restoreRate ` , ` requestedCost ` , ` actualCost ` .
@@ -227,9 +173,9 @@ To quickly get a value, you may pass an optional parameter to the `getApiCalls`
227
173
``` php
228
174
// As example, this will return 79
229
175
// You may pass 'left', 'made', or 'limit'
230
- $left = $api->getApiCalls('left'); // returns 79
176
+ $left = $api->getApiCalls('graph', ' left'); // returns 79
231
177
// or
232
- $left = $api->getApiCalls()['left']; // returns 79
178
+ $left = $api->getApiCalls('graph' )['left']; // returns 79
233
179
```
234
180
235
181
### Isolated API calls
@@ -247,17 +193,17 @@ $api->withSession(string $shop, string $accessToken, Closure $closure);
247
193
` $this ` will be binded to the current API. Example:
248
194
249
195
``` php
250
- $api = new RestAPI (true);
196
+ $api = new BasicShopifyAPI (true);
251
197
$api->setApiKey('your key here');
252
198
$api->setApiPassword('your password here');
253
199
254
200
$api->withSession('some-shop.myshopify.com', 'token from database?', function() {
255
- $request = $this->request ('GET', '/admin/shop.json');
201
+ $request = $this->rest ('GET', '/admin/shop.json');
256
202
echo $request->body->shop->name; // Some Shop
257
203
});
258
204
259
205
$api->withSession('some-shop-two.myshopify.com', 'token from database?', function() {
260
- $request = $this->request ('GET', '/admin/shop.json');
206
+ $request = $this->rest ('GET', '/admin/shop.json');
261
207
echo $request->body->shop->name; // Some Shop Two
262
208
});
263
209
```
0 commit comments