diff --git a/README.md b/README.md index 8459ff7..a5ce4bd 100644 --- a/README.md +++ b/README.md @@ -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/) diff --git a/lib/CurlRequest.php b/lib/CurlRequest.php index b4517e4..2e5ce1e 100644 --- a/lib/CurlRequest.php +++ b/lib/CurlRequest.php @@ -49,6 +49,12 @@ class CurlRequest */ protected static $config = array(); + /** + * User callback to get the response + * @closure + */ + protected static $curlCallback; + /** * Initialize the curl resource * @@ -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); + } + } }