I've created a simple widget to display one post from facebook page. Here is code:
//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;
As You can see I added conditional statement to check if there is photo of post. $img_query_obj['source'] is for posts published normal way - some text + image upload, second - for linked posts - get 130x130 image from data query, because there is no image in standard way.
So my question is, how to get post image if there is only 130x130 image in response?
Greetings
I see two ways.. none of them is perfect..
In "picture" link amongst w=130&h=130&d=ASDDSA... you have &url=http://some.urlencoded.link.to.picture.jpg - so you can download it and resize on tour server..
Ignore "picture" and get "link" - each link (http://some.page/article) is graph object
https://graph.facebook.com/v2.3/http%3A%2F%2Fstackoverflow.com%2F?access_token=YOUR_TOKEN_HERE
{
"og_object": {
"id": "10150180465825637",
"description": "Q&A for professional and enthusiast programmers",
"title": "Stack Overflow",
"type": "website",
"updated_time": "2015-07-10T22:26:23+0000",
"url": "http://stackoverflow.com/"
},
"share": {
"comment_count": 11,
"share_count": 31967
},
"id": "http://stackoverflow.com/"
}
In response you will get og_objet.id and now you can try:
https://graph.facebook.com/v2.3/10150180465825637?access_token=YOUR_TOKEN_HERE
{
"created_time": "2008-04-19T03:48:51+0000",
"title": "Stack Overflow",
"type": "website",
"description": "Q&A for professional and enthusiast programmers",
"image": [
{
"height": 316,
"url": "http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon\u00402.png?v=ea71a5211a91&a",
"width": 316
}
],
"is_scraped": true,
"updated_time": "2015-07-10T22:26:23+0000",
"url": "http://stackoverflow.com/",
"id": "10150180465825637"
}
In response you will have image url, you can get and resize..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With