我有这个代码,得到我在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;
?>
当我像这样打印时:-
<a href="<?php echo $latest_post_link ?>"><?php echo $latest_post_text ?></a>
它只显示我最后的状态。我需要得到最后3个状态。我怎么能做到呢?
你需要循环显示数据
<?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; ?>