Skip to content

Commit b9c02a2

Browse files
author
kshitij verma
committed
update readme file
1 parent b7ae438 commit b9c02a2

File tree

4 files changed

+248
-19
lines changed

4 files changed

+248
-19
lines changed

README.md

Lines changed: 169 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,169 @@
1-
# shopify-api-php-sdk
2-
php-sdk helps to connect with shopify private app and call shopify REST and Graphql Api
1+
# SHOPIFY API PHP SDK
2+
PHP SDK helps to connect with shopify [Custom App](https://shopify.dev/concepts/apps#custom-apps), [Public App](https://shopify.dev/concepts/apps#public-apps) and [Private App](https://shopify.dev/concepts/apps#private-apps) using [RESTApi](https://shopify.dev/docs/admin-api/rest/reference) and [Graphql](https://shopify.dev/docs/admin-api/graphql/reference).
3+
* Call GET, POST, PUT and DELETE RestApi method.
4+
* Process GraphQL Admin API for [Query root](https://shopify.dev/docs/admin-api/graphql/reference/queryroot) and [Mutations](https://shopify.dev/docs/admin-api/graphql/reference/mutation).
5+
* Queryroot is used to get resources and mutations is used to update resources (products/orders/customers).
6+
* Automatic manage Shopify API rate limits.
7+
* Compatible with [Cursor Based Pagination](https://shopify.dev/tutorials/make-paginated-requests-to-rest-admin-api) to resource with pagination.
8+
9+
## Requirements
10+
1. For Api call need [Guzzle](https://github.com/guzzle/guzzle). The recommended way to install Guzzle is through [Composer](https://getcomposer.org/).
11+
```
12+
composer require guzzlehttp/guzzle
13+
```
14+
2. To prepare graphql query with [GraphQL query builder](https://github.com/rudiedirkx/graphql-query).
15+
16+
Clone with Clone with SSH
17+
```
18+
git@github.com:rudiedirkx/graphql-query.git
19+
```
20+
## Getting started
21+
### Initialize the client
22+
#### 1. For Private App
23+
* To create instance of Client, you need `shop`, `api_key`, `password` of private app, `api_params` is an array to pass api version with `YYYY-DD/unstable` format otherwise latest version will be assigned.
24+
25+
```
26+
<?php
27+
require(__DIR__ . '/../vendor/autoload.php');
28+
use Shopify\PrivateApp;
29+
30+
$api_params['version'] = '2019-10';
31+
$client = new Shopify\PrivateApp($shop, $api_key, $password, $api_params);
32+
```
33+
#### 2. For Public/Custom App (under development)
34+
* To create instance of Client, you need `shop`, `api_key`, `api_secret_key` of private app, `api_params` is an array to pass api version with `YYYY-DD/unstable` format otherwise latest version will be assigned.
35+
36+
```
37+
<?php
38+
require(__DIR__ . '/../vendor/autoload.php');
39+
use Shopify\PuplicApp;
40+
41+
$api_params['version'] = '2019-10';
42+
$client = new Shopify\PrivateApp($shop, $api_key, $api_secret_key, $api_params);
43+
```
44+
45+
### Call REST Api
46+
* Get Products with limit 250 with `call()` function
47+
```
48+
$response = $client->call('GET','products',['limit'=>250]);
49+
print_r($response);
50+
```
51+
* Get products of next page with page_info
52+
```
53+
$response = $client->call('GET','products',['limit'=>250]);
54+
```
55+
56+
####### Check next page available with `hasNextPage()` function #######
57+
58+
```
59+
if($client->hasNextPage())
60+
{
61+
$next_page_response $client->call('GET','products',['limit'=>20,'page_info'=>$client->getNextPage()]);
62+
print_r($next_page_response);
63+
}
64+
```
65+
66+
####### Check if previous page available with `hasPrevPage()` function #######
67+
68+
```
69+
if($client->hasPrevPage())
70+
{
71+
$next_page_response $client->call('GET','products',['limit'=>20,'page_info'=>$client->getPrevPage()]);
72+
print_r($next_page_response);
73+
}
74+
```
75+
76+
### Call GraphQL Api
77+
78+
* Get product title, description with id by `query()` function
79+
80+
```
81+
{
82+
product(id: "gid://shopify/Product/1432379031652") {
83+
title
84+
description
85+
}
86+
}
87+
```
88+
89+
####### Prepare query #######
90+
91+
```
92+
<?php
93+
use rdx\graphqlquery\Query;
94+
95+
$query = Query::query("");
96+
$query->fields('product');
97+
$query->product->attribute('id', "gid://shopify/Product/1432379031652");
98+
$query->product->field('title');
99+
$query->product->field('description');
100+
$graphqlString = $query->build();
101+
102+
```
103+
104+
####### Call GraphQL qith `callGraphql()` function #######
105+
106+
```
107+
$response = $client->callGraphql($graphqlString);
108+
```
109+
110+
* Create customer with graphql `mutation()` function
111+
112+
```
113+
mutation {
114+
customerCreate(input: { firstName: "John", lastName: "Tate", email: "john@johns-apparel.com" }) {
115+
customer {
116+
id
117+
}
118+
}
119+
}
120+
```
121+
122+
####### Prepare mutation #######
123+
124+
```
125+
<?php
126+
use rdx\graphqlquery\Query;
127+
128+
$query = Query::mutation();
129+
$query->fields('customerCreate');
130+
$query->customerCreate->attribute('input',['firstName'=>'John','lastName'=> "Tate", 'email'=> "john@johns-apparel.com"]);
131+
$query->customerCreate->field('customer');
132+
$query->customerCreate->customer->field('id');
133+
$graphqlString = $query->build();
134+
```
135+
136+
####### Call GraphQL qith `callGraphql()` function #######
137+
138+
```
139+
$response = $client->callGraphql($graphqlString);
140+
```
141+
142+
### Error Handling
143+
144+
Below errors handled with `ApiException` Class
145+
* Trying to pass invalid api version
146+
* Trying to pass invalid shop domain
147+
* Http REST api call exception from Guzzle(`GuzzleHttp\Exception\RequestException`)
148+
```
149+
try
150+
{
151+
$client = new Shopify\PrivateApp($shop, $api_key, $password, $api_params);
152+
$response = $client->call('GET','products',['limit'=>20]);
153+
print_r($response);
154+
}
155+
catch (\Shopify\Exception\ApiException $e)
156+
{
157+
echo "Errors: ".$e->getError().'<br> status code: '.$e->getCode();
158+
}
159+
```
160+
## References
161+
* [Shopify API Reference](https://shopify.dev/docs/admin-api/)
162+
* [GraphQL Query Builder](https://github.com/rudiedirkx/graphql-query)
163+
* [Guzzle Documentation](http://docs.guzzlephp.org/en/stable/)
164+
165+
166+
167+
168+
169+

src/PrivateApp.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,17 @@ class PrivateApp extends Client implements AppInterface
2929
*/
3030
public function __construct($shop, $api_key, $password, array $api_params = [])
3131
{
32-
try{
33-
$this->setShop($shop);
34-
$this->api_key = $api_key;
35-
$this->password = $password;
36-
$this->api_params = $api_params;
37-
$this->setApiVersion();
38-
$this->prepareBaseUrl();
39-
$this->requestHeaders();
40-
}
41-
catch (ApiException $e)
42-
{
43-
echo "Uncaught exception ".$e->getMessage();
44-
exit();
45-
}
32+
$this->setShop($shop);
33+
$this->api_key = $api_key;
34+
$this->password = $password;
35+
$this->api_params = $api_params;
36+
$this->setApiVersion();
37+
$this->prepareBaseUrl();
38+
$this->requestHeaders();
4639
}
4740

4841
/*
49-
* return Shopify base api url based on api call type
42+
* return Shopify base api url for rest and graphql
5043
* @param array
5144
*/
5245
public function prepareBaseUrl()
@@ -65,7 +58,7 @@ public function prepareBaseUrl()
6558
}
6659

6760
/**
68-
* get request headers for api call
61+
* request header array for rest and graphql api call
6962
* @return array
7063
*/
7164
public function requestHeaders()

src/PublicApp.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
namespace Shopify;
3+
use Shopify\Common\AppInterface;
4+
use Shopify\Exception\ApiException;
5+
6+
/**
7+
* Class PublicApp
8+
* @package Shopify
9+
*/
10+
class PublicApp extends Client implements AppInterface
11+
{
12+
/**
13+
* Shopify rest base url
14+
* @var string
15+
*/
16+
private $rest_api_url = 'https://{shopify_domain}/admin/api/{version}/{resource}.json';
17+
18+
/**
19+
* PublicApp constructor.
20+
* Shopify domain => testshop.myshopify.com
21+
* @param $shop
22+
* Shopify api key
23+
* @param $api_key
24+
* Shopify api secret key
25+
* @param $api_secret_key
26+
* ['version'=>'2020-01']
27+
* @param array $api_params
28+
* @throws ApiException
29+
*/
30+
public function __construct($shop, $api_key, $api_secret_key, array $api_params = [])
31+
{
32+
$this->setShop($shop);
33+
$this->api_key = $api_key;
34+
$this->api_secret_key = $api_secret_key;
35+
$this->api_params = $api_params;
36+
$this->setApiVersion();
37+
$this->prepareBaseUrl();
38+
$this->requestHeaders();
39+
}
40+
41+
/**
42+
* return Shopify base api url for rest and graphql
43+
* @return string
44+
*/
45+
public function prepareBaseUrl()
46+
{
47+
$this->base_urls = [
48+
self::GRAPHQL => strtr($this->graphql_api_url, [
49+
'{shopify_domain}' => $this->shop, '{version}' => $this->getApiVersion()
50+
]),
51+
self::REST_API => strtr($this->rest_api_url, [
52+
'{shopify_domain}' => $this->shop,
53+
'{version}' => $this->getApiVersion(),
54+
])
55+
];
56+
}
57+
58+
/**
59+
* request header array for rest and graphql api call
60+
* @return array
61+
*/
62+
public function requestHeaders()
63+
{
64+
$this->requestHeaders[self::REST_API]['Content-Type'] = "application/json";
65+
$this->requestHeaders[self::REST_API][self::SHOPIFY_ACCESS_TOKEN] = $this->access_token;
66+
$this->requestHeaders[self::GRAPHQL]['Content-Type'] = "application/graphql";
67+
$this->requestHeaders[self::GRAPHQL][self::SHOPIFY_ACCESS_TOKEN] = $this->password;
68+
}
69+
}

test/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
/* examples :
2525
1. Call get product by id
26-
$query = 'query{
26+
$query = '{
2727
product(id: "gid://shopify/Product/1432379031652") {
2828
title
2929
description

0 commit comments

Comments
 (0)