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

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏