【问题标题】:can't get xml child of child using curl_setopt无法使用 curl_setopt 获取孩子的 xml 孩子
【发布时间】:2017-03-01 14:02:24
【问题描述】:

假设我有一个如下所示的 xml 文件:

<objectlist>
    <objectcode>OP#0003</objectcode>
    <objectid>0001</objectid>
    <objecttype>Test object</objecttype>
    <object>
        <info>
            <id>001</id>
            <name>Some name</name>
            <value>5</value>
        </info>
        <properties>
            <shopdetails>
                <desciption>
                    <header>Test</header>
                    <text>This is some text about the object</text>
                </desciption>
                <price>4</price>
                <currency>Dollar</currency>
                <weight>500</weight>
                <gramSymbol>mg</gramSymbol>
            </shopdetails>
        </properties>
    </object>
</objectlist>

如何使用curl_setopt() 从这个 XML 文件中获取所有信息?我用foreach循环尝试过,但失败了。它仅从以下位置获取信息:objectcode、object id 和 objecttype。 我的脚本只能从根的子节点获取信息,但不能从子节点的子节点获取信息。我在我的项目中使用了这个脚本:

    <?php
Class xmlObject{    
    public function xml_From_URL() {
        require_once 'dbconnect.php'; 
        $config[CURLOPT_URL] = "http://localhost/example.xml";
        $config[CURLOPT_VERBOSE] = 0;
        $config[CURLOPT_SSLVERSION] = 3;
        $config[CURLOPT_SSL_VERIFYPEER] = FALSE;
        $config[CURLOPT_SSL_VERIFYHOST] = 2;
        $config[CURLOPT_FOLLOWLOCATION] = 0;
        $config[CURLOPT_HEADER] = 0;
        $config[CURLOPT_RETURNTRANSFER] = 1;
            //-- config section --//
        $tuCurl = curl_init();
        curl_setopt_array($tuCurl, $config);
        $data = curl_exec($tuCurl);
        $xml = simplexml_load_string($data);
            //-- Loops --//
                //-- 1 --//
        foreach($xml -> object as $row){
            $id = $row -> id;
            $name = $row -> name;
            $value = $row -> value;   
            echo("<b>Objects</b></br>");
            echo($id."<br>");
            echo($name."<br>");
            echo($value."<br>");
        }
                //-- 2 --//
        foreach ($xml -> description as $row) {
            $header = $row -> header;
            $text = $row -> text;
            echo("<b>description</b><br>");
            echo($header);
            echo($text);
        }
                //-- 3 --//
        foreach ($xml -> shopdetails as $row) {
            $header = $row -> price;
            $text = $row -> currency;
            $weight = $row -> weight;
            $gramsymbol = $row -> gramsymbol;    
            echo("<b>description</b><br>");
            echo($header);
            echo($text);
        }
        curl_close($tuCurl);
    }
}
?>

【问题讨论】:

    标签: php xml loops oop foreach


    【解决方案1】:

    您只需要更新解析 XML 数据的 PHP 代码 - cURL 只提供 XML 数据,不解析它。您正在使用 simplexml_load_string() 创建一个 SimpleXMLElement。您只需要稍微不同地访问属性。

    而不是像这样从$row 访问id

    $id = $row->id;
    

    我们需要访问&lt;object&gt;&lt;id&gt;之间的节点——即&lt;info&gt;

    $id = $row->info->id;
    

    对于其他属性也是如此。此外,您可以在调用curl_exec 之后立即将curl_close($tuCurl); 向上移动,因为cURL 不解析XML 数据——simplexml 是。您可以在 phpFiddle example 中看到这一点(没有使用 cURL 获取数据)。更多信息请参考PHP documentation for SimpleXML elements on PHP.net

    <?php
    Class xmlObject{    
        public function xml_From_URL() {
            require_once 'dbconnect.php'; 
            $config[CURLOPT_URL] = "http://localhost/example.xml";
            $config[CURLOPT_VERBOSE] = 0;
            $config[CURLOPT_SSLVERSION] = 3;
            $config[CURLOPT_SSL_VERIFYPEER] = FALSE;
            $config[CURLOPT_SSL_VERIFYHOST] = 2;
            $config[CURLOPT_FOLLOWLOCATION] = 0;
            $config[CURLOPT_HEADER] = 0;
            $config[CURLOPT_RETURNTRANSFER] = 1;
            //-- config section --//
            $tuCurl = curl_init();
            curl_setopt_array($tuCurl, $config);
            $data = curl_exec($tuCurl);
    
            //now that we have set $data, we can close the cURL request
            curl_close($tuCurl);
    
            $xml = simplexml_load_string($data);
    
            foreach($xml -> object as $row){ 
                //access these properties from the info childnode  
                $id = $row->info->id;
                $name = $row->info->name;
                $value = $row->info->value;   
                echo("<b>Objects</b></br>");
                echo($id."<br>");
                echo($name."<br>");
                echo($value."<br>");
                //access these properties using the properties childnode
                $header = $row->properties->shopdetails->desciption->header;
                $text = $row->properties->shopdetails->desciption->text;
                echo("<b>description</b><br>");
                echo($header."<br>");
                echo($text."<br>");
            }
        }
    }
    ?>
    

    【讨论】:

    • 感谢它为我正在处理的实际脚本工作。
    猜你喜欢
    • 2018-08-10
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 2014-10-16
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多