phpclassic / php-shopify

PHP SDK for Shopify API
Apache License 2.0
570 stars 210 forks source link

Curl Callback #276

Open whobutsb opened 1 year ago

whobutsb commented 1 year ago

I've been working on extending some of the functionality of the phpclassic/php-shopify library as a Laravel Service Provider. One of the things that I wish I could do for every Curl request is to log the curl headers and response in a centralized place, rather than calling \PHPShopify\CurlRequest::$lastHttpResponseHeaders; After every API request that I make.

So I have added a feature that allows a user to set a callback to receive the CurlResponse information after every response.

Usage:

\PHPShopify\CurlRequest::setCurlCallback(function(CurlResponse $response, CurlHandle $curl) {

    // do something with the curl response data

    \Log::info('Shopify API Response', [
            'shopify_response_headers' => $response->getHeaders(),
             'request_time_elapsed' => curl_getinfo($curl, CURLINFO_TOTAL_TIME),
    ]);
});

I hope others will find this functionality useful. Thank you for the consideration.

UPDATE: Also now including the CurlHandle in the arguments. It is helpful to be able to log the curl requests total elapsed time.

whobutsb commented 1 year ago

Hi @tareqtms, sorry to bother you. I was wondering if this could be considered as a new feature in the library? I've found it be very helpful in my projects to log API requests, especially when talking with Shopify Support who would like information about the request response headers.

Thank you for the consideration.

tareqtms commented 1 year ago

@whobutsb I will merge it soon after checking, thanks.

tareqtms commented 1 year ago

@whobutsb Please check my review comments.

whobutsb commented 1 year ago

@tareqtms - I have resolved the merge conflicts. I'm not seeing any of your comments. I believe you need to submit the comments in the code section in the top right hand corner first for me to see what you commented on. I had this issue before working with other developers.

whobutsb commented 1 year ago

Hi @tareqtms, looking forward to seeing your comments. Could you please submit them so I can review them and fix anything you requested. Thank you very much!

tareqtms commented 1 year ago

Not sure why you can't see the comments. Here is a screenshot.

Screenshot 2023-09-16 at 11 38 18 AM
whobutsb commented 1 year ago

Hi @tareqtms - Thank you for replying with the image and considering this PR. I have been using this curl callback on my own branch for a while and it has been very helpful to retrieve additional details which can be used by Shopify support to help diagnose issues.

The $ch is referring to the CurlHandle object. I used $ch because it is what you have used in your code previously in your CurlRequest.php class. The CurlHandle object is helpful in logging to retrieve additional details about the curl request. In my apps I'am using it to return the full url and also the elapsed time of the request. Example:

'resource_url' => curl_getinfo($ch, CURLINFO_EFFECTIVE_URL),
'time_elapsed' => curl_getinfo($ch, CURLINFO_TOTAL_TIME),

There are also lot so additional helpful methods you can use to get important information about the curl request. See: https://www.php.net/manual/en/function.curl-getinfo.php

I have updated the Example to read "Shopify API Response" to help avoid confusion.

Also I can see you have written the comments but you have not "requested" them so that I can see them. On the "Files changed" page in the top right hand corner you need to submit them with the "request changes" radio button. See: Screenshot 2023-09-18 at 7 41 07 AM