M2中提示 不建议 使用 curl_init 方法,详情请查看下图 可以更换成为magento2中的什么方法呢。
M2中提示 不建议 使用 curl_init 方法,详情请查看下图 可以更换成为magento2中的什么方法呢。
M2中提示 不建议 使用 curl_init 方法,详情请查看下图 可以更换成为magento2中的什么方法呢。
admin 已回答
用m2的Magento\Framework\HTTP\Client\Curl这个类
1,post请求
<?php namespace Codextblog\Demo\Helper; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\HTTP\Client\Curl; class Inventory extends AbstractHelper { /** * @var Curl */ protected $curl; public function __construct( Context $context, Curl $curl, ) { parent::__construct($context); $this->curl = $curl; } public function makeACurlRequest() { $URL = 'www.example.com'; $username = 'username'; $password = 'password'; $jsonData = '{}' //set curl options $this->curl->setOption(CURLOPT_USERPWD, $username . ":" . $password); $this->curl->setOption(CURLOPT_HEADER, 0); $this->curl->setOption(CURLOPT_TIMEOUT, 60); $this->curl->setOption(CURLOPT_RETURNTRANSFER, true); //set curl header $this->curl->addHeader("Content-Type", "application/json"); //post request with url and data $this->curl->post($URL, $jsonData); //read response $response = $this->curl->getBody(); return $response; } }
2,get请求
<?php namespace Codextblog\Demo\Helper; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\HTTP\Client\Curl; class Inventory extends AbstractHelper { /** * @var Curl */ protected $curl; public function __construct( Context $context, Curl $curl, ) { parent::__construct($context); $this->curl = $curl; } public function makeACurlRequest() { $URL = 'www.example.com'; $username = 'username'; $password = 'password'; //set curl options $this->curl->setOption(CURLOPT_USERPWD, $username . ":" . $password); $this->curl->setOption(CURLOPT_HEADER, 0); $this->curl->setOption(CURLOPT_TIMEOUT, 60); $this->curl->setOption(CURLOPT_RETURNTRANSFER, true); $this->curl->setOption(CURLOPT_CUSTOMREQUEST, 'GET'); //set curl header $this->curl->addHeader("Content-Type", "application/json"); //get request with url $this->curl->get($URL); //read response $response = $this->curl->getBody(); return $response; } }
admin 更改状态以发布