Skip to main content

How to call a 3rd party API from a controller? Magento 2

I built a custom module. Its admin tables with CRUD functionality. I am putting a new button on the grid via the Ui_component listing XML file. When this button gets clicked I want to call a 3rd party API to load data into the current table.

This is the code for the button:

<item name="refresh" xsi:type="array">
            <item name="name" xsi:type="string">add</item>
            <item name="label" xsi:type="string" translate="true">refresh</item>
            <item name="class" xsi:type="string">primary</item>
            <item name="url" xsi:type="string">*/*/refreshAction</item>
        </item>

The refraction controller is where I need help with the code to make the API call. For now, I just want to echo out the response, once I get the API call to return a successful response I will deal with actually saving the data to the table. The code I have now that's returning a 500 internal server error looks like this:

public function execute()
{
 $this->_curl->get('https://online.jil.com/api/index.aspx?action=GetAllDIDs');
    $response = $this->_curl->getBody();
    echo $response;
}
R.P.
  • 99
  • 11