Magento2 中如何使用curl
$ url =>包含端点网址。$ params =>它是一个数组,如果你想在url附加额外的参数。$ response =>它将包含json形式的输出。
代码:
/**
* @var \Magento\Framework\HTTP\Client\Curl
*/
protected
$_curl
;
/**
* @param Context $context
* @param \Magento\Framework\HTTP\Client\Curl $curl
*/
public
function
__construct(
Context
$context
,
\Magento\Framework\HTTP\Client\Curl
$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();
}