0

I'm using Linkedin API --> https://developer.linkedin.com/documents/profile-fields#company

Everything is working fine, I can connect to the API with no problem. Bellow follow the data returned by the API when I request some data.

Here is my request (I will not post the whole code, it is more than 500 lines) but it is the essence of what I want to retrieve.

$response2 = $OBJ_linkedin->profile('~:(recommendations-received)');
if($response2['success'] === TRUE) 
{
   $response2['linkedin'] = new SimpleXMLElement($response2['linkedin']);
   echo "<pre>" . print_r($response2['linkedin'], TRUE) . "</pre>";
}
else 
{
   // request failed
   echo "Error: <br />RESPONSE:<br /><br /><pre>" . print_r($response2) . "</pre>";
}

Above follow the response:

SimpleXMLElement Object
(
    [recommendations-received] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [total] => 1
                )

            [recommendation] => SimpleXMLElement Object
                (
                    [id] => 0123456
                    [recommendation-type] => SimpleXMLElement Object
                        (
                            [code] => service-provider
                        )

                    [recommendation-text] => Here come the recommendation text from the API.
                    [recommender] => SimpleXMLElement Object
                        (
                            [id] => npAnFGrTys
                            [first-name] => John
                            [last-name] => Doe
                        )

                )

        )

)

Now my question:

How to retrieve and print only the [recommendation-text] node? I'm already calling only the recommendations-received $response2 = $OBJ_linkedin->profile('~:(recommendations-received)'); but it returns the whole thing, then how to get only the [recommendation-text] ???

I have try with simplexmlelement (http://br2.php.net/manual/en/class.simplexmlelement.php), but with no success.

Thanks in advance for any, any help.

[RESULT]

I will post what was the answer that worked out to me.

I'm using the following code as suggested @Mircea:

$sxml = new SimpleXMLElement($response2['linkedin']); 
$res = $sxml->xpath('recommendations-received/recommendation/recommendation-text'); 
echo $res[0];

Instead of this code:

   $response2['linkedin'] = new SimpleXMLElement($response2['linkedin']);
   echo "<pre>" . print_r($response2['linkedin'], TRUE) . "</pre>";

The difference here is now I'm using the xpath method to search the simpleXML node for children matching. Thanks @Mircea.

2 Answers 2

2

You should try xpath function of simplexmlelement http://www.php.net/manual/en/simplexmlelement.xpath.php. I think that the corect way to get what you want is:

$sxml->xpath('recommendations-received/recommendation/recommendation-text')

It returns an array, so you should iterate on it (look at the examples on that page). The xpath query is something like that, it ultimately depends on the structure of the xml received.

Hope it helps.

Sign up to request clarification or add additional context in comments.

3 Comments

You are right man! $sxml = new SimpleXMLElement($response2['linkedin']); $res = $sxml->xpath('recommendations-received/recommendation/recommendation-text'); echo $res[0];
I will accept your answer as the correct one! Thank you very much, it was so simple that I can't imagine. Thanks again to clear up the idea!
No problem. Glad to be helpful
0

Accessing the recommendation-text property would be done via something like:

foreach($response2->{recommendations-received} as $recommendation) {
  $recommendation->{recommendation-text}
}

2 Comments

I can't retrieve doing like this. Isn't work here. Nothing happen. :(
Thanks anyway, the answer was in using the xpath method to search the node.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.