Hello Magento Friends,

Hope all are doing well. Today I am going to justify Magento 2: How to use Default Curl Class For API Call. In case you missed checking out our previous blog, Magento 2: Add extra “Proceed to Checkout” Button on top of grid on shopping cart page.

Introduction:

Sometimes, developers have a requirement to call third-party API and for that client URL (cURL) request is used. Many developers use the PHP curl function directly and it works perfectly fine. But Magento has already created a curl class for future enhancement. To maintain Magento standards, we must use it. Going against the Magento coding standards will show some warnings.

For e.g, I want to create one function in helper data that is responsible for sending API messages using cURL. Let’s look at the steps to implement it.

Steps to use Default Curl Class For API Call in Magento 2:

Step 1: Create Data.php file in the below path

Vendor\Extension\Helper\Data.php

And add the below code

Conclusion:

Accordingly, one can successfully use the default curl class for API call in Magento 2. In case you face any trouble with the implementation of the above code, mention it in the comment part below. I will be happy to help you out. See you with another solution, till then stay in touch!

Happy Coding!

[Total: 9   Average: 5/5]