系统学习magento二次开发,推荐小册:《Magento中文全栈二次开发 》
本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。
在Magento 2中使用默认curl类进行API调用的步骤:
步骤1:在以下路径中创建数据.php文件
Vendor\Extension\Helper\Data.php
并添加以下代码
<?php
namespace Vendor\Extension\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\HTTP\Client\Curl;
/**
* Class Data
* Vendor\Extension\Helper
*/
class Data extends AbstractHelper
{
/**
* Data constructor.
* @param Curl $curl
*/
public function __construct(
Curl $curl
) {
$this->curl = $curl;
}
/**
* @return string
* @throws LocalizedException
*/
public function defualtCurl()
{
try {
if ($this->isEnable()) {
$postData = [
'first_param' => 'first_value'
];
$this->curl->setOption(CURLOPT_SSL_VERIFYPEER, false);
$this->curl->setOption(CURLOPT_SSL_VERIFYHOST, 2);
// we can add all option which is we used for default PHP curl
$this->curl->post($this->getApiUrl(), $postData);
// Response Text
$response = $this->curl->getBody();
// Response Header
$responseHeader = $this->curl->getHeaders();
// Response cookies
$responseCookies = $this->curl->getCookies();
return $response;
}
} catch (Exception $e) {
$return = ["status"=>false, "message"=> $e->getMessage()];
}
}
}结论:
因此,可以在Magento 2中成功地使用默认的curl类进行API调用。