0

我想从这个 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

4

1 回答 1

1

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

$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"
于 2013-08-27T21:52:37.170 回答