Skip to content
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

Curl Callback #276

Open
wants to merge 5 commits 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: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,22 @@ $config['ShopifyApiFeatures'] = ['include-presentment-prices'];
$shopify = new PHPShopify\ShopifySDK($config);
```

### ShopifySDK Request callback

Sometimes you will want to log all of your requests, you can setup a global logging callback, that will be triggered after the curl request has been made.

```
// set the logging callback
\PHPShopify\CurlRequest::setCurlCallback(function(CurlResponse $response, CurlHandle $handle){

Log::info('Shopify API Response', [
'body' => $response->getBody(),
'headers' => $response->getHeaders(),
'elapsed_time' => curl_getinfo($handle, CURLINFO_TOTAL_TIME),
]);

});


## Reference
- [Shopify API Reference](https://help.shopify.com/api/reference/)
Expand Down
33 changes: 33 additions & 0 deletions lib/CurlRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ class CurlRequest
*/
protected static $config = array();

/**
* User callback to get the response
* @closure
*/
protected static $curlCallback;

/**
* Initialize the curl resource
*
Expand Down Expand Up @@ -216,6 +222,33 @@ protected static function processRequest($ch)

self::$lastHttpResponseHeaders = $response->getHeaders();

// call the user callback with the response
self::callCurlCallback($response, $ch);

return $response->getBody();
}

/**
* set the user callback to be called after the curl request
*
* @param closure $userCallback
* @return void
*/
public static function setCurlCallback($curlCallback)
{
self::$curlCallback = $curlCallback;
}

/**
* call the user callback and pass the response
*
* @param CurlResponse $response
* @return CurlResponse
*/
protected static function callCurlCallback($response, $ch)
{
if (self::$curlCallback) {
return call_user_func(self::$curlCallback, $response, $ch);
}
}
}