1

I want to integrate an external API with Magento 2, The API has given the code for PHP as:

    <?php
// This sample uses the Apache HTTP client from HTTP Components (http://hc.apache.org/httpcomponents-client-ga/)
require_once 'HTTP/Request2.php';

$request = new Http_Request2($apiUrl);
$url = $request->getUrl();

$headers = array(
    // Request headers
    'Key' => 'key',
    'UserName' => 'UserName',
    'Token' => 'Access token'
);

$request->setHeader($headers);

$parameters = array(
    // Request parameters
);

$url->setQueryVariables($parameters);

$request->setMethod(HTTP_Request2::METHOD_POST);

// Request body
$request->setBody("{body}");

try
{
    $response = $request->send();
    echo $response->getBody();
}
catch (HttpException $ex)
{
    echo $ex;
}

?>
1

You can use Guzzle Http client in magento 2 call the API. Every parameters is there but with different name. Please check the link Magento 2 DOC link. https://devdocs.magento.com/guides/v2.4/ext-best-practices/tutorials/create-integration-with-api.html

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.