11<?php
22
33 namespace phpish \shopify ;
4- require_once '../curl/curl.php ' ;
5- use phpish \curl ;
4+ use phpish \http ;
65
76
87 function install_url ($ shop , $ api_key )
@@ -27,57 +26,51 @@ function is_valid_request($query_params, $shared_secret)
2726 }
2827
2928
30- function permission_url ($ shop , $ api_key , $ scope =array (), $ redirect_uri ='' )
29+ function authorization_url ($ shop , $ api_key , $ scopes =array (), $ redirect_uri ='' )
3130 {
32- $ scope = empty ($ scope ) ? '' : '&scope= ' .implode (', ' , $ scope );
31+ $ scopes = empty ($ scopes ) ? '' : '&scope= ' .implode (', ' , $ scopes );
3332 $ redirect_uri = empty ($ redirect_uri ) ? '' : '&redirect_uri= ' .urlencode ($ redirect_uri );
34- return "https:// $ shop/admin/oauth/authorize?client_id= $ api_key$ scope $ redirect_uri " ;
33+ return "https:// $ shop/admin/oauth/authorize?client_id= $ api_key$ scopes $ redirect_uri " ;
3534 }
3635
3736
38- function oauth_access_token ($ shop , $ api_key , $ shared_secret , $ code )
37+ function access_token ($ shop , $ api_key , $ shared_secret , $ code )
3938 {
40- return _api ( ' POST ' , " https:// $ shop/admin/oauth/access_token " , NULL , array ('client_id ' =>$ api_key , 'client_secret ' =>$ shared_secret , 'code ' =>$ code ));
39+ return http \request ( " POST https:// $ shop/admin/oauth/access_token " , array () , array ('client_id ' =>$ api_key , 'client_secret ' =>$ shared_secret , 'code ' =>$ code ));
4140 }
4241
4342
44- function client ($ shop , $ shops_token , $ api_key , $ shared_secret , $ private_app =false )
43+ function client ($ shop , $ shops_token , $ api_key , $ shared_secret , $ private_app =false , $ legacy = false )
4544 {
4645 $ password = $ shops_token ;
47- $ baseurl = "https:// $ shop/ " ;
46+ $ base_uri = $ legacy ? legacy_baseurl ( $ shop , $ api_key , $ shops_token ) : "https:// $ shop " ;
4847
49- return function ($ method , $ path , $ params = array (), & $ response_headers =array ()) use ($ baseurl , $ shops_token )
48+ return function ($ method_uri , $ query = '' , $ payload = '' , & $ response_headers = array (), $ request_headers =array (), $ curl_opts = array ()) use ($ base_uri , $ shops_token )
5049 {
51- $ url = $ baseurl . ltrim ( $ path , ' / ' ) ;
52- $ query = in_array ( $ method , array ( ' GET ' , ' DELETE ' )) ? $ params : array () ;
53- $ payload = in_array ( $ method , array ( ' POST ' , ' PUT ' )) ? stripslashes ( json_encode ( $ params )) : array ( );
50+ $ request_headers [ ' X-Shopify-Access-Token ' ] = $ shops_token ;
51+ $ request_headers [ ' content-type ' ] = ' application/json; charset=utf-8 ' ;
52+ $ http_client = http \client ( $ base_uri , $ request_headers );
5453
55- $ request_headers = array ();
56- array_push ($ request_headers , "X-Shopify-Access-Token: $ shops_token " );
57- if (in_array ($ method , array ('POST ' ,'PUT ' ))) array_push ($ request_headers , "Content-Type: application/json; charset=utf-8 " );
58-
59- return _api ($ method , $ url , $ query , $ payload , $ request_headers , $ response_headers );
60- };
61- }
62-
63- function _api ($ method , $ url , $ query ='' , $ payload ='' , $ request_headers =array (), &$ response_headers =array ())
64- {
6554 try
6655 {
67- $ response = curl \ http_client ($ method , $ url , $ query , $ payload , $ request_headers , $ response_headers );
56+ $ response = $ http_client ($ method_uri , $ query , $ payload , $ response_headers , $ request_headers , $ curl_opts );
6857 }
69- catch (curl \Exception $ e )
58+ catch (http \CurlException $ e ) { throw new CurlException ($ e ->getMessage (), $ e ->getCode (), $ e ->getRequest ()); }
59+ catch (http \ResponseException $ e ) { throw new ApiException ($ e ->getMessage (), $ e ->getCode (), $ e ->getRequest (), $ e ->getResponse ()); }
60+ if (isset ($ response ['errors ' ]))
7061 {
71- throw new CurlException ($ e ->getMessage (), $ e ->getCode ());
62+ list ($ method , $ uri ) = explode (' ' , $ method_uri , 2 );
63+ $ uri = rtrim ($ base_uri ).'/ ' .ltrim ($ uri , '/ ' );
64+ $ headers = $ request_headers ;
65+ $ request = compact ('method ' , 'uri ' , 'query ' , 'headers ' , 'payload ' );
66+ $ response = array ('headers ' =>$ response_headers , 'body ' =>$ response );
67+ throw new ApiException ($ response_headers ['http_status_message ' ].": $ uri " , $ response_headers ['http_status_code ' ], $ request , $ response );
7268 }
7369
74- $ response = json_decode ($ response , true );
75-
76- if (isset ($ response ['errors ' ]) or ($ response_headers ['http_status_code ' ] >= 400 ))
77- throw new ApiException (compact ('method ' , 'path ' , 'params ' , 'response_headers ' , 'response ' , 'shops_myshopify_domain ' , 'shops_token ' ));
78-
7970 return (is_array ($ response ) and !empty ($ response )) ? array_shift ($ response ) : $ response ;
80- }
71+
72+ };
73+ }
8174
8275
8376 function calls_made ($ response_headers )
@@ -105,19 +98,9 @@ function _shop_api_call_limit_param($index, $response_headers)
10598 }
10699
107100
108- class CurlException extends \Exception { }
109- class ApiException extends \Exception
110- {
111- protected $ info ;
112-
113- function __construct ($ info )
114- {
115- $ this ->info = $ info ;
116- parent ::__construct ($ info ['response_headers ' ]['http_status_message ' ], $ info ['response_headers ' ]['http_status_code ' ]);
117- }
118-
119- function getInfo () { $ this ->info ; }
120- }
101+ class Exception extends http \Exception { }
102+ class CurlException extends Exception { }
103+ class ApiException extends Exception { }
121104
122105
123106 function legacy_token_to_oauth_token ($ shops_token , $ shared_secret , $ private_app =false )
0 commit comments