James Miller
James Miller

Reputation: 11

Parsing XML from cURL

I'm trying to parse the mobile.de API. I have this code to get the 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);

I get the following XML Response:

<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>

And the XML object handling:

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

The above code will output:

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

)

How can I echo the data from inside the "ad:foo"?

I stuck at this point!! Was googling for many hours :(

edit:

If I use this code from suggestion I get an empty array:

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

Upvotes: 0

Views: 1277

Answers (3)

Роман Федюк
Роман Федюк

Reputation: 11

Struggled two days with the same problem. First of all - never use print_r or var_dump for debug outputs on SimpleXMLElement. Instead use these functions: https://github.com/IMSoP/simplexml_debug.

Second problem is namespaces in xml. Dirty hack is str_replace to remove the XML namespace: https://www.hacksparrow.com/how-to-manhandle-xml-with-namespace-in-php.html

And final solution, that works like a charm: https://outlandish.com/blog/tutorial/xml-to-json/

Upvotes: 1

Maltronic
Maltronic

Reputation: 1802

You can use SimpleXML's xpath function to search by attributes. e.g.:

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

Upvotes: 1

Jack Wall
Jack Wall

Reputation: 121

The @attributes key will have to be referenced like

$XMLobj->{'@attributes'}

You can iterate over it with a foreach()

Upvotes: 0

Related Questions