-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcishopify.php
More file actions
111 lines (96 loc) · 3.36 KB
/
cishopify.php
File metadata and controls
111 lines (96 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
// Set these values before you start!
define('SHOPIFY_API_KEY', '');
define('SHOPIFY_API_SECRET', '');
define('SHOP_URL','');
define('FORMAT', 'xml');
define('GZIP_ENABLED', true); // set to false if you do not want gzip encoding. If false GZIP_PATH is not needed to be set
define('GZIP_PATH', '/tmp'); // path for gzip decoding (this file will need write permissions)
define('USE_SSL', false);
/* These values only need to be set if USE_SSL is true and the API cannot verify the certificate */
define('USE_SSL_PEM', false); //set to true if pem file is needed
define('CA_FILE', '/full/path/to/cacert.pem');
require_once('shopify_api.php');
/**
* Codeigniter wrapper around the shopify PHP API
*/
class cishopify
{
private $api = NULL;
function __construct()
{
if(SHOPIFY_API_KEY == '' || SHOPIFY_API_SECRET == '' || SHOP_URL == '')
show_error('You need to set your API key, API secret, and the shop URL in the library first');
$this->ci =& get_instance();
$this->ci->load->library('session');
$this->ci->load->database();
$this->api = $this->_getapi();
}
/**
* Authenticates the shop and fetches the token
*
* @return void
* @author Hasitha Pathiraja
*/
function authenticate()
{
$api = new Session(SHOP_URL, '', SHOPIFY_API_KEY, SHOPIFY_API_SECRET);
redirect($api->create_permission_url());
}
/**
* returns a Session object fetched with the token to be used for API calls
*
* @return Session Session object fetched via the token
* @author Hasitha Pathiraja
*/
private function _getapi()
{
$shop = $this->ci->session->userdata('shopify_shop');
$token = $this->ci->session->userdata('shopify_token');
if(!$shop || !$token) $this->authenticate();
return new Session($shop, $token, SHOPIFY_API_KEY, SHOPIFY_API_SECRET);
}
/**
* Sets the token and the shop in the session so that the rest of the library can use it to fetch data
*
* @param string $shop URL of the shop
* @param string $token Token that's returned
* @return void
* @author Hasitha Pathiraja
*/
function setapi($shop,$token)
{
$this->ci->session->set_userdata('shopify_shop',$shop);
$this->ci->session->set_userdata('shopify_token',$token);
}
/*function printapi()
{
$this->api = $this->_getapi();
prettyPrint($this->api);
}*/
/**
* Gets an array of products, optionally belonging to a particular collection
*
* @param string $collection_id The collection id (optional)
* @param array $params Array of additional parameters to pass in
* @param boolean $cache Whether the products should be loaded from cache or not
* @return array Collection of all the products matching the criteria. If no collection id is specified, all products will be returned
* @author Hasitha Pathiraja
*/
function getProducts($collection_id = 0, $params = array(), $cache = false)
{
return $this->api->product->get(0, $collection_id, $params, $cache);
}
/**
* Fetches a product by ID
*
* @param string $id ID of the product that's being fetched
* @param boolean $cache Whether the product should be loaded from cache or not
* @return array Product that matches the id. If no product is found, an array will be returned with an error message
* @author Hasitha Pathiraja
*/
function getProduct($id,$cache=false)
{
return $this->api->product->get($id, 0, array(), $cache);
}
}