PHP从XML中选择属性

时间:2013-08-27 21:37:46

标签: php xml select

我想从这个XML文件中提取:“ENTRY_URL where TITLE = Episodul 1:Pilot”

<?xml version="1.0"?>
<methodResponse>
    <params>
        <param>
            <value>
                <array>
                    <data>
                        <value>
                            <struct>
                                <member>
                                    <name>MATERIAL_ID</name>
                                    <value>
                                        <i4>5464</i4>
                                    </value>
                                </member>
                                <member>
                                    <name>TITLE</name>
                                    <value>
                                        <string>Episodul 1: Pilot</string>
                                    </value>
                                </member>
                                <member>
                                    <name>MESSAGE</name>
                                    <value>
                                        <string></string>
                                    </value>
                                </member>
                                <member>
                                    <name>ENTRY_URL</name>
                                    <value>
                                        <string>
                                            http://www.ufilme.ro/load/under_the_dome_online/sezonul_1/episodul_1_pilot/674-1-0-5464
                                        </string>
                                    </value>
                                </member>
                                <member>
                                    <name>CATEGORY_NAME</name>
                                    <value>
                                        <string>Sezonul 1</string>
                                    </value>
                                </member>
                                <member>
                                    <name>CATEGORY_URL</name>
                                    <value>
                                        <string>http://www.ufilme.ro/load/under_the_dome_online/sezonul_1/674</string>
                                    </value>
                                </member>
                                <member>
                                    <name>COMMENTS_NUM</name>
                                    <value>
                                        <i4>0</i4>
                                    </value>
                                </member>
                                <member>
                                    <name>COMMENTS_URL</name>
                                    <value>
                                        <string>
                                            http://www.ufilme.ro/load/under_the_dome_online/sezonul_1/episodul_1_pilot/674-1-0-5464#comments
                                        </string>
                                    </value>
                                </member>
                                <member>
                                    <name>USER_NAME</name>
                                    <value>
                                        <string>uFilme</string>
                                    </value>
                                </member>
                                <member>
                                    <name>USER_ID</name>
                                    <value>
                                        <i4>1</i4>
                                    </value>
                                </member>
                                <member>
                                    <name>RATING</name>
                                    <value>
                                        <string>0.0</string>
                                    </value>
                                </member>
                                <member>
                                    <name>RATED</name>
                                    <value>
                                        <i4>0</i4>
                                    </value>
                                </member>
                                <member>
                                    <name>READS_NUM</name>
                                    <value>
                                        <i4>1246</i4>
                                    </value>
                                </member>
                                <member>
                                    <name>LOADS_NUM</name>
                                    <value>
                                        <i4>0</i4>
                                    </value>
                                </member>
                                <member>
                                    <name>DATE</name>
                                    <value>
                                        <string>11.07.2013</string>
                                    </value>
                                </member>
                                <member>
                                    <name>TIME</name>
                                    <value>
                                        <string>5:58 PM</string>
                                    </value>
                                </member>
                                <member>
                                    <name>TIMESTAMP</name>
                                    <value>
                                        <i4>1373554697</i4>
                                    </value>
                                </member>
                            </struct>
                        </value>
                        <value>
                            ...
                        </value>
                        ...
                    </data>
                </array>
            </value>
        </param>
    </params>
</methodResponse>

我尝试了在互联网上找到的代码

$xml = simplexml_load_string($catapi);
$results = $xml->xpath('//methodResponse/params/param/array/data/value/struct/member/name[. = "TITLE"];

但它不构成逻辑:|这是一个实例:http://www.ufilme.ro/api/load/under_the_dome_online/sezonul_1/674?apikey=raxeIJAb5ZnwoGn3

1 个答案:

答案 0 :(得分:1)

您拥有的XML不仅仅是XML,它实际上是一个XML-RPC响应,您可以使用名为xmlrpc_decode的相应PHP函数进行解码,然后只需访问该元素:

$filter = function($key, $value) {
    return function(array $entry) use ($key, $value) {
        return isset($entry[$key]) && $entry[$key] === $value;
    };
};

$titleFilter = $filter('TITLE', 'Episodul 1: Pilot');
$array       = xmlrpc_decode(file_get_contents($url), 'utf-8');
$result      = array_filter($array, $titleFilter);

echo array_values($result)[0]['ENTRY_URL']; // prints "http://www.ufilme.ro/load/under_the_dome_online/sezonul_1/episodul_2_the_fire/674-1-0-5465"