From d6b77378eccfbd081221a2fe021c42a15c2ea60f Mon Sep 17 00:00:00 2001 From: Steve Barbera Date: Fri, 11 Nov 2022 11:32:16 -0700 Subject: [PATCH 1/4] Added feature to allow users to create a callback after the curl request is made. --- README.md | 15 +++++++++++++++ lib/CurlRequest.php | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/README.md b/README.md index 6a69c9f..f450901 100644 --- a/README.md +++ b/README.md @@ -520,6 +520,21 @@ $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){ + + Log::info('Shopify API Request', [ + 'body' => $response->getBody(), + 'headers' => $response->getHeaders(), + ]); + +}); + ## Reference - [Shopify API Reference](https://help.shopify.com/api/reference/) diff --git a/lib/CurlRequest.php b/lib/CurlRequest.php index 648296d..f0c5355 100644 --- a/lib/CurlRequest.php +++ b/lib/CurlRequest.php @@ -42,6 +42,12 @@ class CurlRequest */ protected static $config = array(); + /** + * User callback to get the response + * @closure + */ + protected static $curlCallback; + /** * Initialize the curl resource * @@ -208,7 +214,33 @@ protected static function processRequest($ch) self::$lastHttpResponseHeaders = $response->getHeaders(); + // call the user callback with the response + self::callCurlCallback($response); + 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) + { + if (self::$curlCallback) { + return call_user_func(self::$curlCallback, $response); + } + } } From 0a510ca861fcb124a0d81341b53e20ae30f6d3d3 Mon Sep 17 00:00:00 2001 From: Steve Barbera Date: Sat, 12 Nov 2022 12:24:30 -0700 Subject: [PATCH 2/4] Updated to include the curl handle in the callback --- lib/CurlRequest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/CurlRequest.php b/lib/CurlRequest.php index f0c5355..9eb17c3 100644 --- a/lib/CurlRequest.php +++ b/lib/CurlRequest.php @@ -215,7 +215,7 @@ protected static function processRequest($ch) self::$lastHttpResponseHeaders = $response->getHeaders(); // call the user callback with the response - self::callCurlCallback($response); + self::callCurlCallback($response, $ch); return $response->getBody(); } @@ -237,10 +237,10 @@ public static function setCurlCallback($curlCallback) * @param CurlResponse $response * @return CurlResponse */ - protected static function callCurlCallback($response) + protected static function callCurlCallback($response, $ch) { if (self::$curlCallback) { - return call_user_func(self::$curlCallback, $response); + return call_user_func(self::$curlCallback, $response, $ch); } } } From 065c4e9353f49cd693cc75cef4ddf763926644a1 Mon Sep 17 00:00:00 2001 From: Steve Barbera Date: Mon, 18 Sep 2023 07:48:04 -0600 Subject: [PATCH 3/4] Updated the example text to 'Shopify API Response' --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8a9c644..025c0ae 100644 --- a/README.md +++ b/README.md @@ -557,7 +557,7 @@ Sometimes you will want to log all of your requests, you can setup a global logg // set the logging callback \PHPShopify\CurlRequest::setCurlCallback(function(CurlResponse $response){ - Log::info('Shopify API Request', [ + Log::info('Shopify API Response', [ 'body' => $response->getBody(), 'headers' => $response->getHeaders(), ]); From 6121146649121e49d37cd90127909d7b360d617f Mon Sep 17 00:00:00 2001 From: Steve Barbera Date: Wed, 20 Sep 2023 07:38:44 -0600 Subject: [PATCH 4/4] Added example of using the CurlHandle in the curl callback --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 025c0ae..a5ce4bd 100644 --- a/README.md +++ b/README.md @@ -555,11 +555,12 @@ Sometimes you will want to log all of your requests, you can setup a global logg ``` // set the logging callback -\PHPShopify\CurlRequest::setCurlCallback(function(CurlResponse $response){ +\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), ]); });