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