parse linkedin xml in loop



Working with the LinkedIn API to get returned connections from the current user, and I have this working, but am struggling to format the returned data from LinkedIn so that I can save this to a database for further use.


The data returned works fine, and I can print out the entire dump in one go with



$result = new SimpleXMLElement($response['linkedin']);
print_r ($result);


But I want to loop through all entries in the returned data, saving each one to a database as I go.


The returned data is in the format:



SimpleXMLElement Object ( [@attributes] => Array ( [total] => xxx [count] => xxx [start] => 0 ) [person] => Array (

[0] => SimpleXMLElement Object ( [id] => xxx [headline] => xxx [first-name] => xxx [last-name] => xxx [picture-url] => xxx )

[1] => SimpleXMLElement Object ( [id] => xxx [headline] => xxx [first-name] => xxx [last-name] => xxx [picture-url] => xxx )

[2] => SimpleXMLElement Object ( [id] => xxx [headline] => xxx [first-name] => xxx [last-name] => xxx [picture-url] => xxx )


Currently I am just trying to echo the looped data to screen as a test, and my current code is:



$result = new SimpleXMLElement($response['linkedin']);

foreach ($result["data"] as $value) {

$connection_id = $value["id"];
$connection_headline = $value["headline"];
$connection_fname = $value["first-name"];
$connection_lname = $value["last-name"];
$connection_photo = $value["picture-url"];

echo "$connection_id<br />\n";
echo "$connection_fname $connection_lname<br />\n";
echo "$connection_headline<br />\n";
echo "$connection_photo<br /><br />\n";

}


But this doesn't work.


I have also tried the following code from another SO question (How to loop through a LinkedIn API response?)



$result = new SimpleXMLElement($response['linkedin']);

foreach ($result->values as $value) {

$connection_id = $value->id;
$connection_headline = $value->headline;
$connection_fname = $value->first-name;
$connection_lname = $value->last-name;
$connection_photo = $value->picture-url;

echo "$connection_id<br />\n";
echo "$connection_fname $connection_lname<br />\n";
echo "$connection_headline<br />\n";
echo "$connection_photo<br /><br />\n";

}


But this doesn't seem to work either, just resulting in the same blank screen I get with my code.


Any pointers on how to loop through this array of returned 'SimpleXMLElement Object' data, saving each value to a variable as I go, so that I can print to screen / save to database?


Thanks


No comments:

Post a Comment