---
title: "Magento 2: How to use Default Curl Class For API Call"
url: "https://magecomp.com/blog/magento-2-use-default-curl-class-for-api-call/"
date: "2020-12-25T10:00:13+00:00"
modified: "2025-02-20T10:03:00+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 360
reading_time: "2 min read"
summary: "Hello Magento Friends,"
description: "Maintain Magento coding standards by using default Curl class for API call. Find out the steps to execute it"
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to Generate Sitemap in Magento"
    url: "https://magecomp.com/blog/generate-sitemap-magento/"
  - title: "How to Get a value of a Custom attribute of Product in Magento 2 Observer"
    url: "https://magecomp.com/blog/get-value-custom-attribute-product-magento-2-observer/"
  - title: "How to Get Customer Order History using REST API in Magento 2"
    url: "https://magecomp.com/blog/get-customer-order-history-using-rest-api-magento-2/"
---

# Magento 2: How to use Default Curl Class For API Call

_Published: December 25, 2020_  
_Author: Dhiren Vasoya_  

![How to use Default Curl Class For API Call in M2](https://magecomp.com/blog/wp-content/uploads/2020/12/How-to-use-Default-Curl-Class-For-API-Call-in-M2-1024x512.webp)

**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**](https://magecomp.com/blog/magento-2-add-extra-proceed-to-checkout-button-on-top-of-grid-on-shopping-cart-page/).
[![hyva theme development](https://magecomp.com/blog/wp-content/uploads/2025/02/hyva-theme-development-CTA-5.webp)](https://magecomp.com/services/hyva-theme-development/)

## 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
```
<?php
namespace VendorExtensionHelper;

use MagentoFrameworkAppHelperAbstractHelper;
use MagentoFrameworkHTTPClientCurl;

/**
 * Class  Data
 * VendorExtensionHelper
 */
class Data extends AbstractHelper
{
    /**
     * Data constructor.
     * @param Curl $curl
     */
    public function __construct(
        Curl $curl
    ) {
        $this->curl = $curl;
    }

    /**
     * @return string
     * @throws LocalizedException
     */
    public function defualtCurl()
    {
        try {
            if ($this->isEnable()) {
                $postData = [
                    'first_param' => 'first_value'
                ];

                $this->curl->setOption(CURLOPT_SSL_VERIFYPEER, false);
                $this->curl->setOption(CURLOPT_SSL_VERIFYHOST, 2);
		// we can add all option which is we used for default PHP curl
                $this->curl->post($this->getApiUrl(), $postData);
                // Response Text
                $response = $this->curl->getBody();
                // Response Header
                $responseHeader = $this->curl->getHeaders();
                // Response cookies
                $responseCookies = $this->curl->getCookies();
                return $response;
            }
        } catch (Exception $e) {
            $return = ["status"=>false, "message"=> $e->getMessage()];
        }
    }
}
```

## [![Magento version upgrade](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Version-Upgrade-CTA-1.webp)](https://magecomp.com/services/magento-upgrade-services/)

## 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!
[![Magento 2 Speed & Performance Optimization Services](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Speed-Performance-Optimization-Services-CTA-1.webp)](https://magecomp.com/magento-performance-optimization.html)

**Happy Coding!**


---

_View the original post at: [https://magecomp.com/blog/magento-2-use-default-curl-class-for-api-call/](https://magecomp.com/blog/magento-2-use-default-curl-class-for-api-call/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-11-01 16:23:16 UTC_  
