Parsing XML from cURL


Parsing XML from cURL

我正在尝试解析 mobile.de API。我有以下代码来获取 XML:

$titan = TitanFramework::getInstance( 'MWPC' );
$handle = curl_init();
$sellerID = $titan->getOption( 'mwpc_seller_id' ).' HTTP/1.0'; 
$auth_token = base64_encode($titan->getOption( 'mwpc_api_usr' ) . ':' . $titan->getOption( 'mwpc_api_pass' ));
    curl_setopt_array(
        $handle,
        array(
            CURLOPT_URL => 'http://services.mobile.de/1.0.0/ad/search?customerId='.$sellerID,
            CURLOPT_POST => false,
            CURLINFO_CONTENT_TYPE => 'application/xml',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HTTPHEADER => array(
                   'Authorization: Basic '. $auth_token,
                   'accept: application/xml',
                   'Accept-Language: de, en'
            )
        )
    );
$response = curl_exec($handle);

我收到以下 XML 响应:

<search:result xmlns:resource="http://services.mobile.de/schema/resource" xmlns:seller="http://services.mobile.de/schema/seller" xmlns:ad="http://services.mobile.de/schema/ad" xmlns:search="http://services.mobile.de/schema/search" xmlns:financing="http://services.mobile.de/schema/common/financing-1.0" xmlns:error="http://services.mobile.de/schema/common/error-1.0" total="28" page-size="20" current-page="1" max-pages="2">
    <ad:ad key="123456" url="http://services.mobile.de/1.0.0/ad/...">
        <ad:creation-date value="2014-12-08T09:51:39+01:00"/>
        <ad:modification-date value="2015-01-09T12:56:16+01:00"/>
        <ad:detail-page url="http://suchen.mobile.de/auto-inserat/..."/>
        <ad:vehicle>
            <ad:class key="Car" url="http://services.mobile.de/1.0.0/refdata/classes/Car">
                <resource:local-description xml-lang="de">Pkw</resource:local-description>
            </ad:class>
            <ad:category key="Cabrio" url="http://services.mobile.de/1.0.0/refdata/categories/Cabrio">
                <resource:local-description xml-lang="de">Cabrio/Roadster</resource:local-description>
            </ad:category>
            <ad:make key="MINI" url="http://services.mobile.de/1.0.0/refdata/classes/Car/makes/MINI">
                <resource:local-description xml-lang="de">MINI</resource:local-description>
            </ad:make>
            <ad:model key="COOPER_SD_CABRIO" url="http://services.mobile.de/1.0.0/refdata/classes/Car/makes/MINI/models/COOPER_SD_CABRIO">
            <resource:local-description xml-lang="de">Cooper SD Cabrio</resource:local-description>
            </ad:model>

以及 XML 对象处理:

$XML_Obj = simplexml_load_string($response);
echo '<pre>';
print_r($XML_Obj);
echo '</pre>';

上面的代码将输出:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [total] => 28
            [page-size] => 20
            [current-page] => 1
            [max-pages] => 2
        )
)

如何从"ad:foo"内部回显数据?

我卡在这一点上!!谷歌搜索了好几个小时:(

编辑:

如果我使用建议中的这段代码,我会得到一个空数组:

$att = $XML_Obj->xpath("//ad[@key='car']"); 

您可以使用 SimpleXML 的 xpath 函数按属性进行搜索。 例如:

$XML_Obj = simplexml_load_string($response); 
$att = $XML_Obj->xpath("//ad[@key='foo']"); 

同样的问题挣扎了两天。首先 - 切勿使用 print_rvar_dumpSimpleXMLElement 上进行调试输出。请改用这些函数:https://github.com/IMSoP/simplexml_debug。

第二个问题是 xml 中的命名空间。str_replace肮脏的黑客来删除XML命名空间:https://www.hacksparrow.com/how-to-manhandle-xml-with-namespace-in-php.html

最终的解决方案,就像一个魅力:https://outlandish.com/blog/tutorial/xml-to-json/

@attributes键必须像

$XMLobj->{'@attributes'}

你可以用 foreach() 迭代它