0

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

1 Answer 1

0

In this

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

you are looping through and array with an index value of "data" which does not exist in the object.

You are using array syntax on an array when it is an object
That is why I suggest using get_object_vars() to convert the object to an array.

$result = get_object_vars($result);

You have edited the dump you sow so I cannot tell what is what.

do the var_dump after the get_object_vars and post that if you still cannot get it to work.

This might help find the correct indexes:

foreach ($result as $key => $value) {
  $val =  serialize($value)
  echo "<p>key: $key  value: $val</p>"
}

OR

foreach ($result as $key => $value) {
  echo "<p>key: $key  value: " . var_export($value);
}

The first time you run you will get a key. Then use that index value in the foreach loop

 foreach ($result[$key] as $k=> $value) {
Sign up to request clarification or add additional context in comments.

2 Comments

Adding $result = get_object_vars($result); had no effect. Adding var_dump($result); just dumps the whole unformatted response directly to the screen. Neither help in trying to loop through the array and parse values into php variables.
because your dump does not show closing parenthesis, I cannot tell how to get the person array: ` foreach ($????[person]`

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.