drh96824 2016-10-19 19: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 19: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>");
            }
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?