Search code examples
phpxmllinkedin-api

How to parse linkedin xml so I get single fields values


I have this code:

$xml_response = $linkedin->getProfile("~:(id,firstName,lastName,email-address)");

which generates the following result xml:

<person>
<id>c3g9fdgdbP9-</id>
<first-name>Shoen</first-name>
<last-name>Vergue</last-name>
<email-address>[email protected]</email-address>
</person>

How to get for example email value?

I tried this:

$mail=$xml_response['email-address'];

but it returns nothing

Thank you in advance


Solution

  • Check out the SimpleXML Parser, and try something like this:

    libxml_use_internal_errors(true);
    
    $xml = simplexml_load_string($xml_response);
    if ($xml === false) {
        echo "Failed loading XML: ";
        foreach(libxml_get_errors() as $error) {
            echo "<br>", $error->message;
        }
    } else {
        echo $xml->{"email-address"};
    }