2017-05-06 93 views
0

我想在我的Magento2模型中使用CURL。当我打印响应变量时,它不返回和数据。请帮助我了解如何在Magento2的Model中使用CURL?如何在Magento2模型中使用CURL?

型号代码

<?php 
    namespace Inchoo\Helloworld\Model; 

    use \Magento\Framework\Model\AbstractModel; 

    class Orderprocessing extends AbstractModel 
    { 
     const PROCESSING_ID = 'entity_id'; // We define the id fieldname 

     protected $_idFieldName = self::PROCESSING_ID; // parent value is 'id' 

     protected function _construct() 
     { 
      $this->_init('Inchoo\Helloworld\Model\ResourceModel\Orderprocessing'); 
     } 

     public function process($order_data = array()){ 

      if(empty($order_data)){ 
       return false; 
      } 
      $ch = curl_init(); 
      curl_setopt($ch , CURLOPT_URL , $url); 
      curl_setopt($ch , CURLOPT_RETURNTRANSFER , true); 
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
      $response = curl_exec($ch); 
      echo "<pre>"; print_r($response); die; 
     } 
    } 
+0

'$ url'中定义了什么? –

回答

0
  1. 检查方法的这样的说法是不是空的。
  2. 测试curl不同的网址。
0

我发现solution.You可以通过执行卷曲 \在Magento2 Magento的\框架\ HTTP \ ZendClientFactory

<?php 
    namespace Inchoo\Helloworld\Model; 

    use \Magento\Framework\Model\AbstractModel; 

    class Orderprocessing extends AbstractModel 
    { 
     const PROCESSING_ID = 'entity_id'; // We define the id fieldname 

     protected $_idFieldName = self::PROCESSING_ID; // parent value is 'id' 

     protected $_httpClientFactory; 

     /** 
     * Initialize resource model 
     * 
     * @return void 
     */ 
     protected function _construct() 
     { 
      $this->_init('Inchoo\Helloworld\Model\ResourceModel\Orderprocessing'); 
     } 

     public function __construct(\Magento\Framework\HTTP\ZendClientFactory $httpClientFactory){ 
      $this->_httpClientFactory = $httpClientFactory; 
     } 

     public function process($order_data = array()){ 

      $url = "https://wwww.example.com/"; 
      $requestData = array('key' => 'test','password' => '[email protected]', 'action' => 'getProduct', 'id' => 1); 

      $client = $this->_httpClientFactory->create(); 
      $client->setUri($url); 
      $client->setParameterPost($requestData); 
      $client->setHeaders(
       [ 
        'Content-Type' => 'application/x-www-form-urlencoded' 
       ] 
      ); 

      $response = $client->request(\Zend_Http_Client::POST)->getBody(); 
     } 
    }