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