Skip to content

Allow custom HTTP client and add Guzzle adapter #348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ composer require phpclassic/php-shopify
```

### Requirements
PHPShopify uses curl extension for handling http calls. So you need to have the curl extension installed and enabled with PHP.
>However if you prefer to use any other available package library for handling HTTP calls, you can easily do so by modifying 1 line in each of the `get()`, `post()`, `put()`, `delete()` methods in `PHPShopify\HttpRequestJson` class.
PHPShopify relies on the `cURL` extension to perform HTTP requests. Make sure the `cURL` extension is installed and enabled in your PHP environment. Alternatively, you can provide your own HTTP client by implementing the `PHPShopify\HttpClient` interface.

You can pass additional curl configuration to `ShopifySDK`
```php
Expand All @@ -26,6 +25,19 @@ $config = array(
)
);

PHPShopify\ShopifySDK::config($config);
```
## Guzzle or Custom HTTP Client
If you prefer to use a different HTTP library, you can create a custom adapter by implementing the `PHPShopify\HttpClient` interface and passing it via the `HttpClient` configuration option.

For example, using Guzzle is straightforward with the provided adapter:
```php
$client = new GuzzleHttp\Client();
$config = array(
// ...
'HttpClient' => new PHPShopify\GuzzleAdapter($client)
);

PHPShopify\ShopifySDK::config($config);
```
## Usage
Expand Down
2 changes: 1 addition & 1 deletion lib/AuthHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public static function getAccessToken()

$response = HttpRequestJson::post($config['AdminUrl'] . 'oauth/access_token', $data);

if (CurlRequest::$lastHttpCode >= 400) {
if (ShopifySDK::getClient()->getLastResponseCode() >= 400) {
throw new SdkException("The shop is invalid or the authorization code has already been used.");
}

Expand Down
68 changes: 68 additions & 0 deletions lib/CurlAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace PHPShopify;

class CurlAdapter implements HttpClient
{
/**
* @param $url
* @param $headers
*
* @return string
*/
public function get($url, $headers = array())
{
return CurlRequest::get($url, $headers);
}

/**
* @param string $url
* @param array $data
* @param array $headers
*
* @return string
*/
public function post($url, $data = array(), $headers = array())
{
return CurlRequest::post($url, json_encode($data), $headers);
}

/**
* @param string $url
* @param array $data
* @param array $headers
*
* @return string
*/
public function put($url, $data = array(), $headers = array())
{
return CurlRequest::put($url, json_encode($data), $headers);
}

/**
* @param $url
* @param $headers
*
* @return string
*/
public function delete($url, $headers = array())
{
return CurlRequest::delete($url, $headers);
}

/**
* @return int
*/
public function getLastResponseCode()
{
return CurlRequest::$lastHttpCode;
}

/**
* @return array
*/
public function getLastResponseHeaders()
{
return CurlRequest::$lastHttpResponseHeaders;
}
}
4 changes: 2 additions & 2 deletions lib/CurlRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static function get($url, $httpHeaders = array())
* Implement a POST request and return output
*
* @param string $url
* @param array $data
* @param string $data
* @param array $httpHeaders
*
* @return string
Expand All @@ -125,7 +125,7 @@ public static function post($url, $data, $httpHeaders = array())
* Implement a PUT request and return output
*
* @param string $url
* @param array $data
* @param string $data
* @param array $httpHeaders
*
* @return string
Expand Down
151 changes: 151 additions & 0 deletions lib/GuzzleAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

namespace PHPShopify;

class GuzzleAdapter implements HttpClient
{
/**
* Guzzle client instance
*/
protected $client;

protected $lastResponse;

/**
* Pass guzzle http client in constructor
*
* @param $client
*/
public function __construct($client)
{
$this->client = $client;
}

/**
* @param $url
* @param $headers
*
* @return string
*/
public function get($url, $headers = array())
{
$this->lastResponse = $this->client->get($url, [
'headers' => $headers,
]);

return $this->getContents();
}

/**
* @param string $url
* @param array $data
* @param array $headers
*
* @return string
*/
public function post($url, $data = array(), $headers = array())
{
$options = [
'headers' => $headers,
];

if (!empty($data)) {
$options['json'] = $data;
}

$this->lastResponse = $this->client->post($url, $options);

return $this->getContents();
}

/**
* @param string $url
* @param array $data
* @param array $headers
*
* @return string
*/
public function put($url, $data = array(), $headers = array())
{
$options = [
'headers' => $headers,
];

if (!empty($data)) {
$options['json'] = $data;
}

$this->lastResponse = $this->client->put($url, $options);

return $this->getContents();
}

/**
* @param $url
* @param $headers
*
* @return string
*/
public function delete($url, $headers = array())
{
$this->lastResponse = $this->client->delete($url, [
'headers' => $headers,
]);

return $this->getContents();
}

/**
* @return mixed
*/
public function getLastResponse()
{
return $this->lastResponse;
}

/**
* @return int
*/
public function getLastResponseCode()
{
if (empty($this->lastResponse)) {
return 0;
}

return $this->lastResponse->getStatusCode();
}

/**
* @return array
*/
public function getLastResponseHeaders()
{
if (empty($this->lastResponse)) {
return array();
}

$result = array();

foreach ($this->lastResponse->getHeaders() as $name => $value) {
$result[strtolower($name)] = $value[0];
}

return $result;
}

/**
* @return string
*/
protected function getContents()
{
$contents = '';
$body = $this->lastResponse->getBody();

if ($body) {
$contents = $body->getContents();
$body->rewind();
}

return $contents;
}
}
50 changes: 50 additions & 0 deletions lib/HttpClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace PHPShopify;

interface HttpClient
{
/**
* @param string $url
* @param array $headers
*
* @return string
*/
public function get ($url, $headers = array());

/**
* @param string $url
* @param array $data
* @param array $headers
*
* @return string
*/
public function post($url, $data = array(), $headers = array());

/**
* @param string $url
* @param array $data
* @param array $headers
*
* @return string
*/
public function put($url, $data = array(), $headers = array());

/**
* @param string $url
* @param array $headers
*
* @return string
*/
public function delete ($url, $headers = array());

/**
* @return int
*/
public function getLastResponseCode();

/**
* @return array
*/
public function getLastResponseHeaders();
}
2 changes: 2 additions & 0 deletions lib/HttpRequestGraphQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ protected static function prepareRequest($httpHeaders = array(), $data = array()
self::$httpHeaders['Content-type'] = 'application/json';

if (is_array($variables)) {
self::$postData = ['query' => $data, 'variables' => $variables];
self::$postDataGraphQL = json_encode(['query' => $data, 'variables' => $variables]);
} else {
self::$postData = ['query' => $data];
self::$postDataGraphQL = json_encode(['query' => $data]);
}
}
Expand Down
Loading