我是Magento 2的新手,在下面的查询中我需要帮助。
如何在页面加载或单击Magento2.2中的按钮时调用外部API?
我们是否需要为同样的目标创建一个观察者,还是有更好的方法来做到这一点。将感谢任何链接是为逐步提供的过程。
发布于 2019-02-28 01:15:37
首先,我们需要知道如何调用基本API。下面是关于GET的示例:
$externalAPI = 'https://your/external/api_url'
$ch = curl_init($externalAPI);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
$result = curl_exec($ch);
var_dump($result);在Magento方面,我们可以使用\Magento\Framework\HTTP\Client\Curl类:
$apiUrl = '';
$this->curl->addHeader('Content-Type', 'application/json');
$this->curl->get($apiUrl);
$body = $this->curl->getBody();
$httpCode = $this->curl->getStatus();
//Quick decoding body
$dataResponse = \Zend_Json::decode($body);如何在页面加载或单击Magento2.2中的按钮时调用外部API? 我们是否需要为同样的目标创建一个观察者,还是有更好的方法来做到这一点。
这取决于需求。您什么时候需要连接到API?还是直接把它展示在前面?
一旦你知道时间(或地点?)您需要调用外部API。您可以选择插件、观察者或Ajax解决方案。
https://stackoverflow.com/questions/54861986
复制相似问题