Skip to content

How to connect a Magento 2 module to an external API

I created a module in Magento 2 to connect to a third-party API (which I created).
The problem is that it’s not connecting and giving me a 404 error in the view path. local-project.test/getprovider/index/offer
I have created a repository on GitHub with this module to see if you can take a look and give me some ideas.

app/code/Actecnology/GetProvider/view/frontend/layout/offer_index_index.xml

<?xml version="1.0" ?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="ActecnologyGetProviderBlockOffer" name="actecnology.getprovider.offer" template="Actecnology_GetProvider::offer.phtml" />
        </referenceContainer>
    </body>
</page>

app/code/Actecnology/GetProvider/Controller/Index/OfferController.php

<?php

namespace ActecnologyGetProviderControllerIndex;

use MagentoFrameworkAppActionAction;
use MagentoFrameworkAppActionContext;
use MagentoFrameworkControllerResultJsonFactory;

class OfferController extends Action
{
    protected $resultJsonFactory;

    public function __construct(
        Context $context,
        JsonFactory $resultJsonFactory
    ) {
        $this->resultJsonFactory = $resultJsonFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        // die("hola");
        $sku = $this->getRequest()->getParam('sku');

        // Aquí realiza la llamada a la API del proveedor utilizando la librería cURL

        // Llamada a la API utilizando cURL
        $providerUrl = 'http://127.0.0.1:8000/';
        // $apiUrl = $providerUrl . '/getAllSkuOffers/' . $sku;
        $apiUrl = $providerUrl . '/getAllSkuOffers/SKU1';

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $apiUrl);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($curl);
        curl_close($curl);

        // Procesa la respuesta de la API y prepara los datos para ser devueltos

        $result = $this->resultJsonFactory->create();
        return $result->setData(['response' => $response]);
    }
}

app/code/Actecnology/GetProvider/view/frontend/templates/offer.phtml

<div class="offer-container">
    <h2>Ofertas disponibles</h2>
    <?php if ($offers && count($offers) > 0) : ?>
        <ul>
            <?php foreach ($offers as $offer) : ?>
                <li>
                    <h3>ID: <?php echo $offer['id']; ?></h3>
                    <p>Precio: <?php echo $offer['price']; ?></p>
                    <p>Stock: <?php echo $offer['stock']; ?></p>
                    <p>Precio de envío: <?php echo $offer['shipping_price']; ?></p>
                    <p>Fecha de entrega: <?php echo $offer['delivery_date']; ?></p>
                    <p>Puede ser devuelto: <?php echo $offer['can_be_refunded'] ? 'Sí' : 'No'; ?></p>
                    <p>Estado: <?php echo $offer['status']; ?></p>
                    <p>Garantía: <?php echo $offer['guarantee'] ? 'Sí' : 'No'; ?></p>
                    <p>Vendedor: <?php echo $offer['seller']['name']; ?></p>
                    <p>Calificación del vendedor: <?php echo $offer['seller']['qualification']; ?></p>
                    <p>Cantidad de calificaciones: <?php echo $offer['seller']['reviews_quantity']; ?></p>
                </li>
            <?php endforeach; ?>
        </ul>
    <?php else : ?>
        <p>No hay ofertas disponibles en este momento.</p>
    <?php endif; ?>
</div>