Hi Dears
How can I call external API from Magento 2.2.5.
I want integrate magento 2.2.5 to 3rd party to call externall API and allow me to use the payment solution of this system.
Hello @Plat,
You have an experience in Magento 2 to create a module.
Now, Call for external REST API
You have to create an object by using the __construct method. \Magento\Framework\HTTP\ZendClientFactory $httpClientFactory
After you can call external third party REST API by following code
$client = $this->_httpClientFactory->create(); $client->setUri('thirdpartyurl.com/api/'); $client->setMethod(\Zend_Http_Client::PUT); $client->setHeaders(\Zend_Http_Client::CONTENT_TYPE, 'application/json'); $client->setHeaders('Accept','application/json'); $client->setHeaders("Authorization","Bearer 1212121212121"); $client->setParameterPost($params); //json $response= $client->request(); return $response->getBody();
--
If my answer is useful, please Accept as Solution & give Kudos
Hello @Plat
Give a try to this code, I am sure it will work for you to call external REST API in Magento 2.2.5
Hello @manuel_barbosa Give a try to this code, I sure it will work for you to call external REST API in Magento 2.2.5 class YOURCLASS { protected $zendClient; public function __construct( ..... \Zend\Http\Client $zendClient ..... ) { $this->zendClient = $zendClient; } public function yourfunction() { try { $this->zendClient->reset(); $this->zendClient->setUri('YOUR THIRD PARTY API URL'); $this->zendClient->setMethod(\Zend\Http\Request::METHOD_POST); $this->zendClient->setHeaders([ 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'Authorization' => 'Bearer yourvalue', ]); $this->zendClient->setParameterPost([ 'yourparameter1' => 'yourvalue1', ]); $this->zendClient->send(); $response = $this->zendClient->getResponse(); } catch (\Zend\Http\Exception\RuntimeException $runtimeException) { echo $runtimeException->getMessage(); } } } Happy Coding :)
Happy Coding
where to write this code
You can use CURL for executing any API url like follwos:
/** * Constructor. * * @param Magento\Framework\HTTP\Client\Curl $curl */ public function __construct( \Magento\Framework\HTTP\Client\Curl $curl, \Psr\Log\LoggerInterface $logger, ) { $this->curlClient = $curl; $this->logger = $logger; $this->config = $config; } public function createAlert($data) { try { $params = json_encode($data); $this->curlClient->addHeader("Content-Type", "application/json"); $auth = $this->getApiKey(); $this->curlClient->addHeader("Authorization", $auth); $url = $this->getApiUrl() . "/v2/alerts"; // post method $this->curlClient->post($url, $params); // output of curl requestt $result = json_decode($this->getCurlClient()->getBody(), true); $this->logger->info(print_r($result, true)); } catch (\Exception $e) { $this->logger->critical($e->getMessage()); return false; } }
For more information check the official devdoc url : https://devdocs.magento.com/guides/v2.4/get-started/gs-curl.html
Thank you so much for sharing https://devdocs.magento.com/guides/v2.4/get-started/because it helped me a lot to solve my problem. Thanks Again.
thanks!!, it helps
It also helps me alot https://devdocs.magento.com/guides/v2.4/get-started/. This link works for me too!! Thanks Again.
I had same issue before couple of days but now it would be solved by everyone suggestions.
thanks for the information, keep sharing such informative suggesting.
It also helps me alot https://devdocs.magento.com/guides/v2.4/get-started/. This link works for me too!! Thanks Again.