6

I came across two methods for making a CURL call in Magento 2.

Magento\Framework\HTTP\Adapter\Curl

Magento\Framework\HTTP\Client\Curl

Paypal uses the First one as a factory Method to make its NVP cUrl calls and the later one is used by Magento's setup model PackagesAuth class. A few blogs also recommend to use the second one to make our custom cUrl calls.

Which is the recommended method to use?

What is the Difference between these two methods?

1 Answer 1

3

I'll bite...

Essentially there are two ways you can make requests, and which one you use is up to you.

The Curl adapter is an adapter that can be used with \Zend_Http_Client.

The Curl client is a client on its own, and doesn't need an adapter. It's used for scenarios where a different class (that uses composition) expects a \Magento\Framework\HTTP\ClientInterface.

Using ClientInterface seems cleaner, because you can easily switch out the implementation with a different type, and I think Magento makes more use of it.

The Zend Client seems more legacy, but I'm actually using that for a project; it's got more options. i.e. the Curl client only has get and post options available (of course you can overwrite it with the CURLOPT* options, but it's less clean).

I would try to use the Curl client (indirectly), because it implements ClientInterface; it seems to be where Magento is moving to. The Magento_Payment module uses it extensively as well as Magento_Braintree.

Magento_Braintree seems to have been built to serve as an example for other developers that are building payment gateway integrations (as well as providing Braintree functionality of course).

It's also possible the ClientInterface is more user-friendly when it's used with Virtual Types and DI.

If using a ClientInterface implementation doesn't work out, you can always switch to \Zend_Http_Client with an adapter of your choosing.

I hope this wasn't too vague and provided some info!

3
  • In M2.3.2 I see Magento\Braintree uses its own http client implementation full of plain curl_***() functions calls, found in \Braintree\Http at vendor/braintree/braintree_php/lib/Braintree/Http.php Sep 4, 2019 at 9:54
  • In M2.3.2 I see Magento\Payment uses Magento\Framework\HTTP\ZendClient found in \Magento\Payment\Gateway\Http\Client\Zend::placeRequest Sep 4, 2019 at 10:05
  • Hmm maybe this changed at some point, my answer was from a year and half ago. I'm still confused by it :')
    – Erfan
    Sep 8, 2019 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.