让以下数组在网站上完美运行,但在第一次输出之前打印“数组”一词。
我认为它必须在$ json_object或$ fbdata查询中,但不能隔离或消除它显示。
<?php
$page_id = '{page_id_here}';
$access_token = '{access_token_here}';
//Get the JSON
$json_object = @file_get_contents('https://graph.facebook.com/' . $page_id .
'/posts?fields=full_picture,link,message&limit=3&access_token=' .
$access_token);
//Interpret data
$fbdata = json_decode($json_object);
foreach ($fbdata->data as $post )
{
$posts .= '<div class="col-sm-4">';
$posts .= '<div class="stay-connected-inner">';
$posts .= '<div class="stay-connected-info">';
$posts .= '<div class="stay-connected-left"><i class="fa fa-facebook"></i></div>';
$posts .= '<div class="stay-connected-right">';
$posts .= '<h5>Title</h5>';
$posts .= '<p><a href="' . $post->link . '" target="_blank">' . $post->message . '</a></p>';
$posts .= '</div>';
$posts .= '</div>';
$posts .= '<div class="stay-connected-fig">';
$posts .= '<p><a href="' . $post->link . '" target="_blank"><img src="' . $post->full_picture . '"></a></p>';
$posts .= '</div>';
$posts .= '</div>';
$posts .= '</div>';
}
//Display the posts
print_r ($posts)
?>
答案 0 :(得分:0)
在$posts
循环之前将foreach
声明为字符串。
<?php
$page_id = '{page_id_here}';
$access_token = '{access_token_here}';
//Get the JSON
$json_object = @file_get_contents('https://graph.facebook.com/' . $page_id .
'/posts?fields=full_picture,link,message&limit=3&access_token=' .
$access_token);
//Interpret data
$fbdata = json_decode($json_object);
$posts = ''; // Add this
foreach ($fbdata->data as $post )
{
$posts .= '<div class="col-sm-4">';
$posts .= '<div class="stay-connected-inner">';
$posts .= '<div class="stay-connected-info">';
$posts .= '<div class="stay-connected-left"><i class="fa fa-facebook"></i></div>';
$posts .= '<div class="stay-connected-right">';
$posts .= '<h5>Title</h5>';
$posts .= '<p><a href="' . $post->link . '" target="_blank">' . $post->message . '</a></p>';
$posts .= '</div>';
$posts .= '</div>';
$posts .= '<div class="stay-connected-fig">';
$posts .= '<p><a href="' . $post->link . '" target="_blank"><img src="' . $post->full_picture . '"></a></p>';
$posts .= '</div>';
$posts .= '</div>';
$posts .= '</div>';
}
//Display the posts
print_r ($posts)
?>
否则它会将$posts
转换为数组,因此在打印时,它还会打印出它是一个数组。
答案 1 :(得分:0)
想出来。我将'posts'变量改为'postst'。 “post”这个词正在URL中使用,似乎在某种程度上混淆了数组。
更新的代码:
<?php
$page_id = '{page_id_here}';
$access_token = '{access_token_here}';
//Get the JSON
$json_object = @file_get_contents('https://graph.facebook.com/' . $page_id .
'/posts?fields=full_picture,link,message&limit=3&access_token=' .
$access_token);
//Interpret data
$fbdata = json_decode($json_object);
foreach ($fbdata->data as $post )
{
$postst .= '<div class="col-sm-4">';
$postst .= '<div class="stay-connected-inner">';
$postst .= '<div class="stay-connected-info">';
$postst .= '<div class="stay-connected-left"><i class="fa fa-facebook"></i></div>';
$postst .= '<div class="stay-connected-right">';
$postst .= '<h5>Title</h5>';
$postst .= '<p><a href="' . $post->link . '" target="_blank">' . $post->message . '</a></p>';
$postst .= '</div>';
$postst .= '</div>';
$postst .= '<div class="stay-connected-fig">';
$postst .= '<p><a href="' . $post->link . '" target="_blank"><img src="' . $post->full_picture . '"></a></p>';
$postst .= '</div>';
$postst .= '</div>';
$postst .= '</div>';
}
//Display the posts
print_r ($postst);
?>