Magento2 中如何使用curl

$ url =>包含端点网址。$ params =>它是一个数组,如果你想在url附加额外的参数。$ response =>它将包含json形式的输出。
 
代码:
/**
 * @var MagentoFrameworkHTTPClientCurl
 */
protected $_curl;
 
/**
 * @param Context                             $context
 * @param MagentoFrameworkHTTPClientCurl $curl
 */
public function __construct(
    Context $context,
    MagentoFrameworkHTTPClientCurl $curl
) {
    $this->_curl = $curl;
    parent::__construct($context);
}
 
public function execute()
{
    //if the method is get
    $this->_curl->get($url);
    //if the method is post
    $this->_curl->post($url, $params);
    //response will contain the output in form of JSON string
    $response = $this->_curl->getBody();
}
原文地址:https://www.cnblogs.com/shenlongjue/p/7525097.html