【问题标题】:Get XML from https URL从 https URL 获取 XML
【发布时间】:2009-12-11 14:03:38
【问题描述】:

我正在尝试从多个 XML 提要中获取数据并将该数据复制到我的本地数据库。我已经尝试研究 SimpleXML 和我在互联网上找到的其他一些东西,但我想知道使用这种东西的最佳途径是什么。

我正在寻找的东西不仅可以从安全位置获取 XML,而且可以将其转换为一系列数组。

【问题讨论】:

    标签: php xml https parsing


    【解决方案1】:

    这是一个非常简单的过程,您可以使用 CURL 和某种 XML 到数组类来完成。我会在这里给你详细介绍。

    PHP:

    /* run mozilla agent */
    $agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_USERAGENT, $agent); //make it act decent
    curl_setopt($ch, CURLOPT_URL, $url);         //set the $url to where your request goes
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //set this flag for results to the variable
    curl_setopt($ch, CURLOPT_POST, 1);           //if you're making a post, put the data here
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post); //as a key/value pair in $post
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //This is required for HTTPS certs if
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //you don't have some key/password action
    
    /* execute the request */
    $result = curl_exec($ch);
    curl_close($ch);
    
    /* now parse the resulting XML using XMLToArray class from 
    Razzaque Rupom http://groups.yahoo.com/group/phpresource/ */
    require_once('lib/class.XmlToArray.php');
    $parser = new XmlToArray($result);
    $data = $parser->createArray();
    

    你有它。

    • 米奇

    【讨论】:

    【解决方案2】:

    使用SimpleXML 时,您将XML 作为一个包含节点数组的对象。我个人更喜欢使用 SimpleXML 而不是任何其他 XML 解析器,因为它的简单性和整体性能。它在操作数据时很容易用作 DOM 操纵器,在用作 XPath 解析器时也很容易只检索几个节点。

    如果您遍历一个非常大的 XML 文件,您可能会更好地使用加载惰性的直接 SAX 解析器,但由于您是通过网络阅读,我相信性能差异可以忽略不计。

    【讨论】:

      【解决方案3】:

      使用 cURL (http://php.net/manual/en/ref.curl.php) 从 https 位置获取数据,然后使用 simplexml_load_string 加载它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-26
        • 1970-01-01
        相关资源
        最近更新 更多