Skip to content

Commit

Permalink
Added support for report center
Browse files Browse the repository at this point in the history
  • Loading branch information
martinamps committed Mar 1, 2013
1 parent 3c48167 commit 2abd554
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 14 deletions.
136 changes: 135 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ if (!$api->isLoggedIn() && !$api->login()) {

print_r($do_discount);


# List coupons
$params = [
'limit' => 50,
Expand All @@ -59,9 +58,144 @@ if (!$api->isLoggedIn() && !$api->login()) {
print_r($coupon);
}
}

$params = [
'reportcenter' => true,
'token' => $api->dashboardToken(),
'start_date' => '2013-02-22',
'end_date' => '2013-03-01',
'timezone' => 'Pacific+Time+(US+%26+Canada)'
];

$referrals = $api->doRequest('GET', 'referrals.json', $params);
print_r($referrals);

$facts = $api->doRequest('GET', 'facts.json', $params);
print_r($facts);

$periodical_facts = $api->doRequest('GET', 'periodical_facts.json', $params);
print_r($periodical_facts);
}
```

## Sample Output

### Create Token
```php

stdClass Object
(
[discount] => stdClass Object
(
[applies_once] =>
[applies_to_id] =>
[code] => automated_token_example
[ends_at] =>
[id] => 16956508
[minimum_order_amount] => 0.00
[starts_at] => 2013-03-01T00:00:00-08:00
[status] => enabled
[usage_limit] => 1
[value] => 5.0
[discount_type] => percentage
[applies_to_resource] =>
[times_used] => 0
)

)
```

### Get Token
```php
(
stdClass Object
(
[applies_once] =>
[applies_to_id] =>
[code] => automated_token_example
[ends_at] =>
[id] => 16956508
[minimum_order_amount] => 0.00
[starts_at] => 2013-03-01T00:00:00-08:00
[status] => enabled
[usage_limit] => 1
[value] => 5.0
[discount_type] => percentage
[applies_to_resource] =>
[times_used] => 0
)

...
)

```

### Referrals
```php
stdClass Object
(
[start_date] => 2013-02-22
[end_date] => 2013-03-01
[search_terms] => Array
(
[0] => stdClass Object
(
[terms] => shopify.com
[count] => 1
[percentage] => 100
)

)

[top_referrals] => Array
(
[0] => stdClass Object
(
[referrer] => www.example.com
[count] => 530
[percentage] => 56.025369978858
)

....
)
)
```

### Facts
```php
stdClass Object
(
[start_date] => 2013-02-22
[end_date] => 2013-03-01
[facts] => stdClass Object
(
[orders] => xxx4
[visits] => xxx83
[customers] => xxx0
[unique_visits] => xxx8
[revenue_per_visitor] => x2.600408834586
[revenue_per_customer] => xx9.10291304348
[revenue_order_average] => xxx9.34515748031
[repeat_customer_percentage] => 29.4488188976378
[revenue] => xxxxxxx8
)

[conversions] => stdClass Object
(
[total] => stdClass Object
(
[count] => xxx3
[percentage] => 100
)

[cart] => stdClass Object
(
[count] => xxx1
[percentage] => 40.663436451733
)

```

## Notes

Use at your own risk, enjoy!
52 changes: 39 additions & 13 deletions shopify.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@

class PrivateAPI {
const _LOGIN_URL = 'auth/login';

const _REPORT_CENTER = 'https://reportcenter.shopify.com/';

const _COOKIE_STORE = '/tmp/shopify_cookie.txt';
const _USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17';

protected $ch = null;
protected $ci = null;
protected $ch = null,
$ci = null;

private $inputs = false,
$username = false,
$password = false,
$store = false,
$token = false;
$_token = false,
$_dashboardToken = false;

public function __construct($user, $pass, $store) {
if (!filter_var($store, FILTER_VALIDATE_URL))
Expand All @@ -24,7 +26,6 @@ public function __construct($user, $pass, $store) {
$this->store = $store . (substr($store, -1) == '/' ? '' : '/');
$this->username = $user;
$this->password = $pass;

}

public function __destruct() {
Expand All @@ -36,6 +37,10 @@ public function isLoggedIn() {
return !is_array($this->getFields());
}

public function dashboardToken() {
return $this->_dashboardToken;
}

public function login() {
$fields = $this->inputs ?: $this->getFields();

Expand All @@ -60,9 +65,17 @@ public function login() {

public function doRequest($method, $function, $parameters) {
$this->ch = curl_init();

$url = $this->store . $function;

$reportCenter = false;

if (isset($parameters['reportcenter'])) {
$url = self::_REPORT_CENTER . $function;
$reportCenter = true;
$parameters['callback'] = 'fake_function';
unset($parameters['reportcenter']);
} else {
$url = (!filter_var($function, FILTER_VALIDATE_URL) ? $this->store : '') . $function;
}

switch ($method) {
case 'POST':
$this->setOpts([
Expand All @@ -71,7 +84,7 @@ public function doRequest($method, $function, $parameters) {
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => [
'X-Shopify-Api-Features: pagination-headers',
'X-CSRF-Token: ' . $this->token,
'X-CSRF-Token: ' . $this->_token,
'X-Requested-With: XMLHttpRequest',
'Content-Type: application/json',
'Accept: application/json'
Expand All @@ -92,25 +105,39 @@ public function doRequest($method, $function, $parameters) {
if (curl_errno($this->ch))
throw new \Exception('Shopify Private API exception: ' . curl_error($this->ch));

return json_decode($response);
if ($reportCenter) {
if (strpos($response, 'fake_function') !== FALSE) {
$response = substr($response, strpos($response, '{'));
$response = substr($response, 0, -2);
}
}

$data = json_decode($response);

return is_object($data) ? $data : $response;
}

public function setToken($input) {
$data = filter_var($input, FILTER_VALIDATE_URL) ? $this->initGetData($input) : $input;

if (preg_match('/<meta content="(.*)" name="csrf-token" \/>/i', $data, $token)) {
$this->token = $token[1];
$this->_token = $token[1];

if (preg_match('/Shopify.set\(\'controllers.dashboard.token\', "(.*)"\)/i', $data, $dashboardToken)) {
$this->_dashboardToken = $dashboardToken[1];
}

return true;
}


throw new \Exception('Failed to set token');
}

private function initGetData($url, $opts = []) {
if (!filter_var($url, FILTER_VALIDATE_URL))
throw new \Exception('Invalid URL: ' . $url);


$this->ch = curl_init($url);
$this->setOpts($opts);

Expand All @@ -123,7 +150,6 @@ private function initGetData($url, $opts = []) {
}

private function setOpts($extra = []) {

$default = [
CURLOPT_USERAGENT => self::_USER_AGENT,
CURLOPT_COOKIEJAR => self::_COOKIE_STORE,
Expand Down

0 comments on commit 2abd554

Please sign in to comment.