0

I have this code that get me last status on facebook:-

<?php 
/*---------------------------------------------------------------------------------*/
# Gets the data from a URL
/*---------------------------------------------------------------------------------*/
function get_data($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
  $data = get_data("[url]");
  $result = json_decode($data);

  $latest_post =  $result->data[0];
  $latest_post_text = $latest_post->message;
  $latest_post_link = $latest_post->actions[0]->link;

?>

When I print like this :-

<a href="<?php echo $latest_post_link ?>"><?php echo $latest_post_text ?></a>

Its Show me only last status. i need to get me last 3 status. how can i do that ??

0

1 Answer 1

1

you will need loop to show data

<?php 
  $counter = 0; // Add a counter to your code
  foreach ($result->data as $latest):
  $counter++; //Icrement your counter
  if ($counter > 3) { break; } // Stop foreach after 3 loops
?> 

<a href="<?php echo $latest->actions[0]->link; ?>"><?php echo $latest->message; ?></a>

<?php endforeach; ?> 
Sign up to request clarification or add additional context in comments.

2 Comments

Nice but it get me all status, How can get only 3 from this loop
I added a break after tree occurrences in this example, so now it should only return 3

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.