Skip to content

Commit e585aad

Browse files
committed
Initial commit
1 parent 5b702d4 commit e585aad

File tree

3 files changed

+404
-0
lines changed

3 files changed

+404
-0
lines changed

README.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# About
2+
3+
A simple PHP wrapper around the [Shopify API](https://help.shopify.com/api/getting-started).
4+
5+
## Installation
6+
7+
Install via [Composer](https://getcomposer.org/) by running `composer require luketowers/php-shopify-api` in your project directory.
8+
9+
## Usage
10+
11+
In order to use this wrapper library you will need to provide credentials to access Shopify's API.
12+
13+
You will either need an access token for the shop you are trying to access (if using a [public application](https://help.shopify.com/api/getting-started/authentication#public-applications)) or an API Key and Secret for a [private application](https://help.shopify.com/api/getting-started/authentication#private-applications).
14+
15+
## Examples
16+
17+
#### Make an API call
18+
```php
19+
use LukeTowers\ShopifyPHP\Shopify;
20+
21+
// Initialize the client
22+
$api = new Shopify('exampleshop.myshopify.com', 'mysupersecrettoken');
23+
24+
// Get all products
25+
$result = $api->call('GET', 'admin/products.json');
26+
27+
// Get the products with ids of '632910392' and '921728736' with only the 'id', 'images', and 'title' fields
28+
$result = $api->call('GET', 'admin/products.json', [
29+
'ids' => '632910392,921728736',
30+
'fields' => 'id,images,title',
31+
]);
32+
33+
// Create a new "Burton Custom Freestyle 151" product
34+
$result = $api->call('POST', 'admin/products.json', [
35+
'product' => [
36+
"title" => "Burton Custom Freestyle 151",
37+
"body_html" => "<strong>Good snowboard!</strong>",
38+
"vendor" => "Burton",
39+
"product_type" => "Snowboard",
40+
"tags" => 'Barnes & Noble, John's Fav, "Big Air"',
41+
],
42+
]);
43+
```
44+
45+
#### Use Private Application API Credentials to authenticate API requests
46+
```php
47+
use LukeTowers\ShopifyPHP\Shopify;
48+
49+
$api = new Shopify($data['shop'], [
50+
'api_key' => '...',
51+
'secret' => '...',
52+
]);
53+
```
54+
55+
#### Use an access token to authenticate API requests
56+
```php
57+
use LukeTowers\ShopifyPHP\Shopify;
58+
59+
$storedToken = ''; // Retrieve the stored token for the shop in question
60+
$api = new Shopify('exampleshop.myshopify.com', $storedToken);
61+
```
62+
63+
#### Request an access_token for a shop
64+
```php
65+
use LukeTowers\ShopifyPHP\Shopify;
66+
67+
function make_authorization_attempt($shop, $scopes)
68+
{
69+
$api = new Shopify($shop, [
70+
'api_key' => '...',
71+
'secret' => '...',
72+
]);
73+
74+
$nonce = bin2hex(random_bytes(10));
75+
76+
// Store a record of the shop attempting to authenticate and the nonce provided
77+
$storedAttempts = file_get_contents('authattempts.json');
78+
$storedAttempts = $storedAttempts ? json_decode($storedAttempts) : [];
79+
$storedAttempts[] = ['shop' => $shop, 'nonce' => $nonce, 'scopes' => $scopes];
80+
file_put_contents('authattempts.json', json_encode($storedAttempts));
81+
82+
return $api->getAuthorizeUrl($scopes, 'https://example.com/handle/shopify/callback', $nonce);
83+
}
84+
85+
header('Location: ' . make_authorization_attempt('exampleshop.myshopify.com', ['read_product']));
86+
die();
87+
```
88+
89+
#### Handle Shopify's response to the authorization request
90+
```php
91+
use LukeTowers\ShopifyPHP\Shopify;
92+
93+
function check_authorization_attempt()
94+
{
95+
$data = $_GET;
96+
97+
$api = new Shopify($data['shop'], [
98+
'api_key' => '...',
99+
'secret' => '...',
100+
]);
101+
102+
$storedAttempt = null;
103+
$attempts = json_decode(file_get_contents('authattempts.json'));
104+
foreach ($attempts as $attempt) {
105+
if ($attempt->shop === $data['shop']) {
106+
$storedAttempt = $attempt;
107+
break;
108+
}
109+
}
110+
111+
return $api->authorizeApplication($storedAttempt->nonce, $data);
112+
}
113+
114+
$response = check_authorization_attempt();
115+
if ($response) {
116+
// Store the access token for later use
117+
$response->access_token;
118+
}
119+
```

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "luketowers/php-shopify-api",
3+
"description": "PHP wrapper for Shopify API",
4+
"keywords": ["PHP","Shopify","API"],
5+
"homepage": "https://github.com/luketowers/php-shopify-api",
6+
"type": "library",
7+
"require": {
8+
"php": ">=7.0",
9+
"guzzlehttp/guzzle": "~6.0"
10+
},
11+
"license": "MIT",
12+
"authors": [
13+
{
14+
"name": "Luke Towers",
15+
"email": "[email protected]"
16+
}
17+
],
18+
"autoload": {
19+
"psr-4": {
20+
"LukeTowers\\ShopifyPHP\\": "src/"
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)