Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sandeepshetty committed Jun 19, 2012
0 parents commit b185f05
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
19 changes: 19 additions & 0 deletions LICENSE.txt
@@ -0,0 +1,19 @@
Copyright (C) 2011 by Sandeep Shetty

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
20 changes: 20 additions & 0 deletions composer.json
@@ -0,0 +1,20 @@
{
"name": "sandeepshetty/shopify_client",
"type": "library",
"description": "Simple Shopify API client in PHP",
"keywords": ["shopify","api"],
"homepage": "https://github.com/sandeepshetty/shopify_client",
"license": "MIT",
"authors": [
{
"name": "Sandeep Shetty",
"email": "sandeep.shetty@gmail.com",
"homepage": "http://sandeep.shetty.in",
"role": "Developer"
}
],
"require": {
"php": ">=5.3.0",
"sandeepshetty/wcurl": "dev-master"
}
}
120 changes: 120 additions & 0 deletions shopify_client.php
@@ -0,0 +1,120 @@
<?php

require '../wcurl/wcurl.php';

function install_url($shop, $api_key)
{
return "http://$shop/admin/api/auth?api_key=$api_key";
}

function is_valid_request($query_params, $shared_secret)
{
$seconds_in_a_day = 24 * 60 * 60;
$older_than_a_day = $query_params['timestamp'] < (time() - $seconds_in_a_day);
if ($older_than_a_day) return false;

$signature = $query_params['signature'];
unset($query_params['signature']);

foreach ($query_params as $key=>$val) $params[] = "$key=$val";
sort($params);

return (md5($shared_secret.implode('', $params)) === $signature);
}


function permission_url($shop, $api_key, $scope=array(), $redirect_uri='')
{
$scope = empty($scope) ? '' : '&scope='.implode(',', $scope);
$redirect_uri = empty($redirect_uri) ? '' : '&redirect_uri='.urlencode($redirect_uri);
return "https://$shop/admin/oauth/authorize?client_id=$api_key$scope$redirect_uri";
}


function oauth_access_token($shop, $api_key, $shared_secret, $code)
{
return shopify_api('POST', "https://$shop/admin/oauth/access_token", NULL, array('client_id'=>$api_key, 'client_secret'=>$shared_secret, 'code'=>$code));
}


function shopify_api($method, $url, $query='', $payload='', $request_headers=array(), &$response_headers=array())
{
try
{
$response = wcurl($method, $url, $query, $payload, $request_headers, $all_response_headers);
}
catch(WcurlException $e)
{
throw new ShopifyClientCurlException($e->getMessage(), $e->getCode());
}

$response = json_decode($response, true);
$response_headers = array_pop($all_response_headers);

if (isset($response['errors']) or ($response_headers['http_status_code'] >= 400))
throw new ShopifyClientApiException(compact('method', 'path', 'params', 'response_headers', 'response', 'shops_myshopify_domain', 'shops_token'));

return (is_array($response) and !empty($response)) ? array_shift($response) : $response;
}


function shopify_client($shop, $shops_token, $api_key, $secret, $private_app=false)
{
print_r(get_defined_vars());
//$password = $private_app ? $secret : md5($secret.$shops_token);
$password = $shops_token;
$baseurl = "https://$shop/";
//$baseurl = "https://$api_key:$password@$shops_myshopify_domain/";

return function ($method, $path, $params=array(), &$response_headers=array()) use ($baseurl, $password)
{
$url = $baseurl.ltrim($path, '/');
$query = in_array($method, array('GET','DELETE')) ? $params : array();
$payload = in_array($method, array('POST','PUT')) ? stripslashes(json_encode($params)) : array();

$request_headers = array();
array_push($request_headers, "X-Shopify-Access-Token: $password");
if (in_array($method, array('POST','PUT'))) array_push($request_headers, "Content-Type: application/json; charset=utf-8");

return shopify_api($method, $url, $query, $payload, $request_headers, $response_headers);
};
}


function shopify_calls_made($response_headers)
{
return shopify_shop_api_call_limit_param_(0, $response_headers);
}

function shopify_call_limit($response_headers)
{
return shopify_shop_api_call_limit_param_(1, $response_headers);
}

function shopify_calls_left($response_headers)
{
return shopify_call_limit($response_headers) - shopify_calls_made($response_headers);
}

function shopify_shop_api_call_limit_param_($index, $response_headers)
{
$params = explode('/', $response_headers['http_x_shopify_shop_api_call_limit']);
return (int) $params[$index];
}


class ShopifyClientCurlException extends Exception { }
class ShopifyClientApiException extends Exception
{
protected $info;

function __construct($info)
{
$this->info = $info;
parent::__construct($info['response_headers']['http_status_message'], $info['response_headers']['http_status_code']);
}

function getInfo() { $this->info; }
}

?>

0 comments on commit b185f05

Please sign in to comment.