Skip to content

Commit b185f05

Browse files
committed
Initial commit
0 parents  commit b185f05

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed

LICENSE.txt

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (C) 2011 by Sandeep Shetty
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

composer.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "sandeepshetty/shopify_client",
3+
"type": "library",
4+
"description": "Simple Shopify API client in PHP",
5+
"keywords": ["shopify","api"],
6+
"homepage": "https://github.com/sandeepshetty/shopify_client",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Sandeep Shetty",
11+
"email": "[email protected]",
12+
"homepage": "http://sandeep.shetty.in",
13+
"role": "Developer"
14+
}
15+
],
16+
"require": {
17+
"php": ">=5.3.0",
18+
"sandeepshetty/wcurl": "dev-master"
19+
}
20+
}

shopify_client.php

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
require '../wcurl/wcurl.php';
4+
5+
function install_url($shop, $api_key)
6+
{
7+
return "http://$shop/admin/api/auth?api_key=$api_key";
8+
}
9+
10+
function is_valid_request($query_params, $shared_secret)
11+
{
12+
$seconds_in_a_day = 24 * 60 * 60;
13+
$older_than_a_day = $query_params['timestamp'] < (time() - $seconds_in_a_day);
14+
if ($older_than_a_day) return false;
15+
16+
$signature = $query_params['signature'];
17+
unset($query_params['signature']);
18+
19+
foreach ($query_params as $key=>$val) $params[] = "$key=$val";
20+
sort($params);
21+
22+
return (md5($shared_secret.implode('', $params)) === $signature);
23+
}
24+
25+
26+
function permission_url($shop, $api_key, $scope=array(), $redirect_uri='')
27+
{
28+
$scope = empty($scope) ? '' : '&scope='.implode(',', $scope);
29+
$redirect_uri = empty($redirect_uri) ? '' : '&redirect_uri='.urlencode($redirect_uri);
30+
return "https://$shop/admin/oauth/authorize?client_id=$api_key$scope$redirect_uri";
31+
}
32+
33+
34+
function oauth_access_token($shop, $api_key, $shared_secret, $code)
35+
{
36+
return shopify_api('POST', "https://$shop/admin/oauth/access_token", NULL, array('client_id'=>$api_key, 'client_secret'=>$shared_secret, 'code'=>$code));
37+
}
38+
39+
40+
function shopify_api($method, $url, $query='', $payload='', $request_headers=array(), &$response_headers=array())
41+
{
42+
try
43+
{
44+
$response = wcurl($method, $url, $query, $payload, $request_headers, $all_response_headers);
45+
}
46+
catch(WcurlException $e)
47+
{
48+
throw new ShopifyClientCurlException($e->getMessage(), $e->getCode());
49+
}
50+
51+
$response = json_decode($response, true);
52+
$response_headers = array_pop($all_response_headers);
53+
54+
if (isset($response['errors']) or ($response_headers['http_status_code'] >= 400))
55+
throw new ShopifyClientApiException(compact('method', 'path', 'params', 'response_headers', 'response', 'shops_myshopify_domain', 'shops_token'));
56+
57+
return (is_array($response) and !empty($response)) ? array_shift($response) : $response;
58+
}
59+
60+
61+
function shopify_client($shop, $shops_token, $api_key, $secret, $private_app=false)
62+
{
63+
print_r(get_defined_vars());
64+
//$password = $private_app ? $secret : md5($secret.$shops_token);
65+
$password = $shops_token;
66+
$baseurl = "https://$shop/";
67+
//$baseurl = "https://$api_key:$password@$shops_myshopify_domain/";
68+
69+
return function ($method, $path, $params=array(), &$response_headers=array()) use ($baseurl, $password)
70+
{
71+
$url = $baseurl.ltrim($path, '/');
72+
$query = in_array($method, array('GET','DELETE')) ? $params : array();
73+
$payload = in_array($method, array('POST','PUT')) ? stripslashes(json_encode($params)) : array();
74+
75+
$request_headers = array();
76+
array_push($request_headers, "X-Shopify-Access-Token: $password");
77+
if (in_array($method, array('POST','PUT'))) array_push($request_headers, "Content-Type: application/json; charset=utf-8");
78+
79+
return shopify_api($method, $url, $query, $payload, $request_headers, $response_headers);
80+
};
81+
}
82+
83+
84+
function shopify_calls_made($response_headers)
85+
{
86+
return shopify_shop_api_call_limit_param_(0, $response_headers);
87+
}
88+
89+
function shopify_call_limit($response_headers)
90+
{
91+
return shopify_shop_api_call_limit_param_(1, $response_headers);
92+
}
93+
94+
function shopify_calls_left($response_headers)
95+
{
96+
return shopify_call_limit($response_headers) - shopify_calls_made($response_headers);
97+
}
98+
99+
function shopify_shop_api_call_limit_param_($index, $response_headers)
100+
{
101+
$params = explode('/', $response_headers['http_x_shopify_shop_api_call_limit']);
102+
return (int) $params[$index];
103+
}
104+
105+
106+
class ShopifyClientCurlException extends Exception { }
107+
class ShopifyClientApiException extends Exception
108+
{
109+
protected $info;
110+
111+
function __construct($info)
112+
{
113+
$this->info = $info;
114+
parent::__construct($info['response_headers']['http_status_message'], $info['response_headers']['http_status_code']);
115+
}
116+
117+
function getInfo() { $this->info; }
118+
}
119+
120+
?>

0 commit comments

Comments
 (0)