drh96824 2016-10-19 11:13
浏览 32
已采纳

使用curl_setopt无法获取子项的xml子项

Say I have a xml file like the one below:

<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>

How do I get all the information out of this XML file using curl_setopt()? I tried it with a foreach loop, but it failed. It only gets the information from: objectcode, object id and objecttype. My script is only able to get information from childnodes of the root but not children of child nodes. I used this script for my project:

    <?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);
    }
}
?>

展开全部

  • 写回答

1条回答 默认 最新

  • duandan9680 2016-10-19 11:40
    关注

    You just need to update your PHP code that parses the XML data - cURL only provides the XML data, doesn't parse it. You are using simplexml_load_string() to create a SimpleXMLElement. You just need to access properties a little differently.

    Instead of accessing id from $row like this:

    $id = $row->id;
    

    we need to access the node between <object> and <id> - i.e. <info>

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

    And similarly for the other properties. Also, you can move the curl_close($tuCurl); up right after the call to curl_exec since cURL isn't parsing the XML data - simplexml is. You can see this in action (without the data fetching with cURL) in this phpFiddle example. For more information, refer to the 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>");
            }
        }
    }
    ?>
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部