Skip to content

Commit d6b7737

Browse files
committed
Added feature to allow users to create a callback after the curl request is made.
1 parent b815f08 commit d6b7737

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,21 @@ $config['ShopifyApiFeatures'] = ['include-presentment-prices'];
520520
$shopify = new PHPShopify\ShopifySDK($config);
521521
```
522522

523+
### ShopifySDK Request callback
524+
525+
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.
526+
527+
```
528+
// set the logging callback
529+
\PHPShopify\CurlRequest::setCurlCallback(function(CurlResponse $response){
530+
531+
Log::info('Shopify API Request', [
532+
'body' => $response->getBody(),
533+
'headers' => $response->getHeaders(),
534+
]);
535+
536+
});
537+
523538
524539
## Reference
525540
- [Shopify API Reference](https://help.shopify.com/api/reference/)

lib/CurlRequest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ class CurlRequest
4242
*/
4343
protected static $config = array();
4444

45+
/**
46+
* User callback to get the response
47+
* @closure
48+
*/
49+
protected static $curlCallback;
50+
4551
/**
4652
* Initialize the curl resource
4753
*
@@ -208,7 +214,33 @@ protected static function processRequest($ch)
208214

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

217+
// call the user callback with the response
218+
self::callCurlCallback($response);
219+
211220
return $response->getBody();
212221
}
213222

223+
/**
224+
* set the user callback to be called after the curl request
225+
*
226+
* @param closure $userCallback
227+
* @return void
228+
*/
229+
public static function setCurlCallback($curlCallback)
230+
{
231+
self::$curlCallback = $curlCallback;
232+
}
233+
234+
/**
235+
* call the user callback and pass the response
236+
*
237+
* @param CurlResponse $response
238+
* @return CurlResponse
239+
*/
240+
protected static function callCurlCallback($response)
241+
{
242+
if (self::$curlCallback) {
243+
return call_user_func(self::$curlCallback, $response);
244+
}
245+
}
214246
}

0 commit comments

Comments
 (0)