如何在 magento 1.9 模块中使用 cURL

How to use cURL in magento 1.9 module

我开发了一个模块,目前只有一些功能和get/post从数据库,前面我已经完成了。 我想添加一个 class (cURL) 来调用第 3 方 api,但我不知道如何实现它。它应该作为新文件位于控制器(IndexController?)或模型文件夹中?如果我必须在 config.xml?

中添加一些详细信息

我建议您在助手 class 中添加您的 cURl 并从控制器调用该助手方法。 无需在 config.xml 中添加任何细节。只需在 config.xml 中定义您的助手 class 并在辅助函数

中添加 cURL 数据

在特定文件中使用 curl 没有限制。这取决于您的模块功能和参数。最有效的努力是在 indexcontroller 中使用 curl。使用可以创建一个函数并启动它们。

例如:

  <?php

    $curl = new Varien_Http_Adapter_Curl();
    $curl->setConfig(array(
           'timeout'   => 15    //Timeout in no of seconds
    ));
    $feed_url = "http://feeds.feedburner.com/magento";
    $curl->write(Zend_Http_Client::GET, $feed_url, '1.0');
    $data = $curl->read();
    if ($data === false) {
       return false;
    }
    $data = preg_split('/^r?$/m', $data, 2);
    $data = trim($data[1]);
    $curl->close();

    try {
      $xml  = new SimpleXMLElement($data);
     //Parse the XML FEED and output the data
    }
    catch (Exception $e) {
       echo $e->getMessage();
    }

    ?>