0

We are trying to create a admin grid using UI Component and need to fetch data from external rest API, not able to figure-out the exact file from where we can inject this json data

Any help is highly appreciated

Thanks in Advance.

1
+50

please follow this guide for creating a new UI component backend. https://www.mageplaza.com/devdocs/creat-a-ui-form-in-magento-2.html

Once the UI component is created, check here how you create your custom source data: https://devdocs.magento.com/guides/v2.4/ui_comp_guide/concepts/ui_comp_data_source.html

Your custom suorce data should not be a standard collection, but it will have to call a class that executes the specific curl call you need. This data will then be injected through the dataProvider.

It is not easy to explain it is a complex process if developed according to magento specifications

Cheers

1
  • yes this seems to be the right approach have implemented something similar Aug 5 '20 at 17:55
-1

For this you had to install a module. Declare a public URL in the module. access that URL and inside the "execute" function of controller, call external rest API with curl command.

app/code/vendorname/modulname/Controller/Fetchdata.php

<?php
namespace vendorname\modulname\Controller;

class Fetchdata extends \Magento\Framework\App\Action\Action
{
    protected $_pageFactory;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $pageFactory)
    {
        $this->_pageFactory = $pageFactory;
        return parent::__construct($context);
    }

    public function execute()
    {
        //execute Curl call to external rest api here.
        $curl = curl_init();
        curl_setopt_array($curl, array(
          CURLOPT_URL => "http://api.minetools.eu/ping/play.desnia.net/25565",
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_TIMEOUT => 30,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => "GET",
          CURLOPT_HTTPHEADER => array(
          "cache-control: no-cache"
        ),
       ));

       $response = curl_exec($curl);
       $err = curl_error($curl);

       curl_close($curl);


        //than push response from curl call to insert data into grid table 
        //using mysql query


        $this->_resources = \Magento\Framework\App\ObjectManager::getInstance()
->get('Magento\Framework\App\ResourceConnection');
       $connection= $this->_resources->getConnection();

       $themeTable = $this->_resources->getTableName('yourtablename');
       $sql = "INSERT INTO " . $themeTable . "(field1, field2) VALUES ('1', 'NameABC')";
       $connection->query($sql);
    }
}

for publick URL, you can use this file.

app/code/vendorname/modulename/etc/frontend/routes.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
    <router id="standard">
        <route id="fetch_rest_api_data" frontName="fetch_rest_api_data">
            <module name="vendorname_modulename" />
        </route>
    </router>
</config>

Now in browser, run this url http://yourmagentourl.com/fetch_rest_api_data/fetchdata i-e http://yourmagentourl.com/RoutFrontName/ControllerName

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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