我创建了一个简单的小部件,用于显示Facebook页面中的一篇帖子。以下是代码:
:
//VARS
$title = $instance['title'];
$app_id = $instance['add_id'];
$app_secret = $instance['app_secret'];
$page_id = $instance['pade_id'];
$my_url = $instance['my_url'];
$page_name = $instance['page_name'];
$message_lenght = $instance['message_lenght'];
//CONFIGS
// get user access_token
$token_url = 'https://graph.facebook.com/oauth/access_token?client_id='
. $app_id . '&redirect_uri=' . urlencode($my_url)
. '&client_secret=' . $app_secret
. '&grant_type=client_credentials';
// response is of the format "access_token=AAAC..."
$access_token = substr(file_get_contents($token_url), 13);
$fql_query_url = 'https://graph.facebook.com/v2.3/' . $page_id
. '/posts?access_token=' . $access_token;
$fql_query_result = file_get_contents($fql_query_url);
$fql_query_obj = json_decode($fql_query_result, true);
$data = $fql_query_obj['data'][0];
$img_query_url = 'https://graph.facebook.com/v2.3/' . $data['object_id'] . '/?access_token=' . $access_token;
$img_query_result = file_get_contents($img_query_url);
$img_query_obj = json_decode($img_query_result, true);
$msg = substr($data['message'], 0, $message_lenght). '... ';
if($img_query_obj['source'] == ""){
$img_url = $data[picture];
}else{
$img_url = $img_query_obj['source'];
}
//START APP
echo $before_widget;
if($title !== ""){
echo '<h3>'.$title.'</h3>';
}
?>
<div class="facebook-widget">
<pre class="hide"><?php var_dump($data[picture]); ?></pre>
<div class="img"><img src="<?php echo $img_url; ?>"></div>
<div class="text">
<div class="date">
FACEBOOK /
<?php echo date("m.d.Y", strtotime($data['updated_time'])); ?>
</div>
<a class="page_title" target="_blank" href="<?php echo $my_url; ?>">/<?php echo $page_name; ?></a>
<p><?php echo mb_substr($msg, 0, 95); ?>[...]<a href="<?php echo $data[link]; ?>" target="_blank"> (read more)</a></p>
</div>
<div class="facebookfollow">
<a class="facebook-fallow-button" href="<?php echo $my_url; ?>" target="_blank">Follow Us</a>
</div>
</div>
<?php
echo $after_widget;
正如您所看到的,我添加了条件语句以检查是否有帖子的照片。 $img_query_obj ['source']用于以正常方式发布的帖子-一些文本+图像上传,第二个用于链接的帖子-从数据查询中获取130x130图像,因为标准方式中没有图像。
因此,我的问题是,如果响应中只有130x130的图像,则如何获取帖子图像?
问候