• xml simpleXML_load_file(), simpleXML_load_string()


    xml.xml文件

    <?xml version='1.0'?>
    <man>
        <att>
            <name>lin3615</name>
            <sex>M</sex>
            <age>26</age>
        </att>
        <att>
            <name>lin361500</name>
            <sex>mmm</sex>
            <age>20</age>
        </att>
    </man>

    用simpleXML_load_file()实现

    <?php
    $ff = 'http://localhost/test.xml';
    $str = simpleXML_load_file($ff);
    print_r($str);
    foreach($str->att as $v) print_r($v);

    function get_contents($url){
         if (ini_get("allow_url_fopen") == "1") {
                    $response = file_get_contents($url);
            }else{
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
                    curl_setopt($ch, CURLOPT_URL, $url);
                    $response =  curl_exec($ch);
                    curl_close($ch);
            }

            return $response;
    }

    用simpleXML_load_string()实现

    <?php
    $ff = get_contents("http://localhost/test/test.xml");
    $str = simpleXML_load_string($ff);
    print_r($str);
    foreach($str->att as $v) print_r($v);

    function get_contents($url){
         if (ini_get("allow_url_fopen") == "1") {
                    $response = file_get_contents($url);
            }else{
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
                    curl_setopt($ch, CURLOPT_URL, $url);
                    $response =  curl_exec($ch);
                    curl_close($ch);
            }

            return $response;
    }

    结果都为:

    SimpleXMLElement Object
    (
        [att] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [name] => lin3615
                        [sex] => M
                        [age] => 26
                    )

                [1] => SimpleXMLElement Object
                    (
                        [name] => lin361500
                        [sex] => mmm
                        [age] => 20
                    )

            )

    )
    SimpleXMLElement Object
    (
        [name] => lin3615
        [sex] => M
        [age] => 26
    )
    SimpleXMLElement Object
    (
        [name] => lin361500
        [sex] => mmm
        [age] => 20
    )

  • 相关阅读:
    QQ 2006 正式版协议变动情况分析
    开源3D方面的软件
    要使silverlight能够在IIS中顺利运行,需要设置的东西.
    文本框回车自动提交
    No installed Service named "Apache2"
    Javascript:三个函数ceil()、floor()、round()的区别
    基于Apache的Svn服务器配置
    下载:HD AUDIO For XP SP3修正补丁
    ViewState在执行Refresh后丢失
    C#关闭应用程序时如何关闭子线程
  • 原文地址:https://www.cnblogs.com/lin3615/p/3876669.html
Copyright © 2020-2023  润新知