<coded>


Create Mobile.de PHP script to receive seller data

May 16, 2022

When it comes to APIs sometimes its hard to find the perfect piece of code for your project. A lot of services today offer either free or paid APIs for working with their data. Mobile.de is a great example for this. Working on a project, I needed to display all the car data of the seller on Wordpress.

First problem? All plugins were pretty bloated and not 100% what I needed. I wanted it very clean, so I decided to dive into the api and run my own piece of code.

All the api docs can be found here: https://services.mobile.de/manual/index.html

What we use is the Search-API with ad-integration, since it is free to use in your seller portal. We don't want the Seller-API, this is only possible as a paid service.

1. Activate the Search-API in your Mobile.de account

Before we start you need to get your login credentials. English terms and wording can vary a bit here. Go to navigation tab Sell and go for Data export, data import.

Mobile.de - Step 1

Next click the button on the the top navigation and then Create new password.

Mobile.de - Step 1

2. Create a PHP function to crawl the data and cache it

We create a simple function with curl to fetch the data from the api. What we also do is, cache the received data to a xml file to turn down the api requests as much as we can. We don't want to do useless requests everytime the page loads.

First we define the file path for our cache file. I called it mobile.xml and did set the file modify interval to 3600 seconds, what makes it 60 minutes refresh time for every api request. If we are below 3600 seconds it will use the cache file to read the data, if we are above the value it will send an api request to fetch the most current data.

After that we define the endpoint url with your customerID. You can find all the information in the official api link above. For the basic auth just add your username and password, seperated by a colon and Base64 encode it.

<?php
function mobile_de_crawler() {
    $file = $_SERVER["DOCUMENT_ROOT"]."/wp-content/themes/understrap-child/api/mobile.xml";

    if((time()-filemtime($file) > 3600) || !file_exists($file)) {
        $url = "https://services.mobile.de/search-api/search?customerId=YOUR_CUSTOMER_ID";

        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        $headers = array(
           "Accept: application/xml",
           "Authorization: Basic ".base64_encode("username:password"),
           "Accept-Encoding: gzip",
           "Accept-Language: de",
           "Content-Type: application/xml",
        );
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

        $result = curl_exec($curl);
        curl_close($curl);

        $fx = fopen($file, "w+");
        $write = fwrite($fx, $result);
        fclose($fx);
    }
    else {
        $fp = fopen($file, "r");
        $result = fread($fp, filesize($file));
        fclose($fp);
    }

    return $result;
}
?>

3. Let's start with parsing the data

Because we get xml data, the easiest for me was just looping through with xpath.

<?php
function parse() {
    $data = mobile_de_crawler();
    $xml = simplexml_load_string($data);

    $ads = $xml->xpath("//search:search-result/search:ads/ad:ad");

    ...

Here is an example with the loop I used. Since there is so much data in the xml, I can't show everything in detail here, but you will easily figure out how to parse through everything with the example:

    ...

    foreach($ads as $ad) {
        $data_detailpage = $ad->xpath("ad:detail-page")[0]->attributes()->url;

        $adDetails = $ad->xpath("ad:vehicle");

        foreach($adDetails as $detail) {
            $data_class = $detail->xpath("ad:class/resource:local-description")[0];
            $data_category = $detail->xpath("ad:category/resource:local-description")[0];
            $data_make = $detail->xpath("ad:make/resource:local-description")[0];
            $data_model = $detail->xpath("ad:model/resource:local-description")[0];
            $data_model_desc = $detail->xpath("ad:model-description")[0]->attributes()->value;
            $data_mileage = $detail->xpath("ad:specifics/ad:mileage");

            $data_seats = $detail->xpath("ad:specifics/ad:num-seats");
            (!empty($data_seats)) ? $data_seats = $data_seats[0]->attributes()->value." seats" : $data_seats = "No data";

            $data_fuel = $detail->xpath("ad:specifics/ad:fuel/resource:local-description");
            (!empty($data_fuel)) ? $data_fuel = $data_fuel[0] : $data_fuel = "No data";

            $data_power = $detail->xpath("ad:specifics/ad:power");
            (!empty($data_power)) ? $data_power = $data_power[0]->attributes()->value." PS" : $data_power = "No data";

            $data_gearbox = $detail->xpath("ad:specifics/ad:gearbox/resource:local-description");
            (!empty($data_gearbox)) ? $data_gearbox = $data_gearbox[0] : $data_gearbox = "No data";

            $data_condition = $detail->xpath("ad:specifics/ad:condition/resource:local-description");
            (!empty($data_condition)) ? $data_condition = $data_condition[0] : $data_condition = "No data";
        }

        $data_image = $ad->xpath("ad:images/ad:image/ad:representation")[3]->attributes()->url;
        $data_price = $ad->xpath("ad:price/ad:consumer-price-amount")[0]->attributes()->value;
        $data_price = number_format(intval($data_price), 0, ",", ".")." €";

        return "
            <li class="example-car-data">
                <h2>".$data_model." - ".$data_make"</h2>
                <p>".$data_class."</p>
                <p>".$data_seats."</p>
                <p>".$data_fuel."</p>
                <p class="big-text">".$data_price."</p>
            </li>
        ";
    }
}
?>

You can wrap all the data in <li> tags and then into an <ul> to display it with a slider or just plain list it on your website. Just call the echo parse(); function and the script will return the data.

4. Display the data on your site

When you have figured out what you want to display and you are looking for a good and clean slider integration, I can highly recommend https://glidejs.com/. It's by far the best script I used.

If you want to go further and display single ads, this is also possible. You just create a new function, same as mobile_de_crawler(); and modify it by changing the endpoint url to:

https://services.mobile.de/search-api/ad/{ad-key}

Then the api will return the information of a single ad only. You will need to loop through the xml dom with xpath again.

5. That's it

Maybe you can extend it for your needs. This might be just a quick and easy way to get some Mobile.de data ready. I didn't need it to be a huge Class for this, but you can of course wrap it into one.

Did you find this useful? Please rate this post: