samedi 18 avril 2015

Facebook graph api like, share, comment relative to facebook post

I have an open graph feed from a Facebook page.



/page/feed?fields=from{picture{url},name},to,story,created_time,message,attachments,comments{from{picture{url},name},created_time,message},actions


I would like to know if there is a way to display a like button, share button or comment box on my website where the like, share or comment is posted relative to a facebook post, instead of the page or first attachment of the post


http://ift.tt/1jGR5ie


http://ift.tt/1bXGyrk


http://ift.tt/1otAWwg


All of the above links when passed the facebook post url will either resolve to the page that the post is attached to or the first attachment in the post and not the actual post itself


The code



<?php
define('FACEBOOK_SDK_V4_SRC_DIR', '../facebook-php-sdk-v4-4.0-dev/src/Facebook/');
require '../facebook-php-sdk-v4-4.0-dev/autoload.php';

use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;

FacebookSession::setDefaultApplication($app_id,$app_secret);
FacebookSession::enableAppSecretProof(false);

$session = new FacebookSession($access_key);
$request = new FacebookRequest($session,'GET',"/{$page}/feed?fields=object_id,from{picture{url},name},to,story,created_time,message,attachments,comments{from{picture{url},name},created_time,message},actions");
$response = $request->execute();
$graphObject = $response->getGraphObject();
function get_fb_time($date) {
$date = strtotime($date);
$difference = time() - $date;
$periods = array(
'second' => 1,
'minute' => 60,
'hour' => 3600);
if($difference <= 86400) {
foreach ($periods as $key => $value) {
if ($difference >= $value) {
$time = floor($difference/$value);
$retval = "$time " . (($time > 1) ? $key . 's' : $key);
}
}
} else {
$retval = date('F j \a\t g:ia', $date);
}
return $retval;
}
function get_fb_attachment($data) {
$content = "<div class='fb-attachment-container' style='display:inline-block;'>";
$content .= "<a href='{$data->url}' style='border-radius:inherit;'>";
$content .= '<div class="panel panel-default fb-attachment" style="margin-bottom:0px;margin-top:15px;display:inline-block;border-radius-inherit;">';
//if($data->type != 'photo' && $data->type != 'album' && $data->type != 'cover_photo') {
if(isset($data->title)) $content .= "<div style='font-weight: bold;color:rgb(75,125,175);padding:15px;margin:0px;border-bottom:1px solid #ddd;background:#f6f7f8;border-top-left-radius:inherit;border-top-right-radius:inherit;'>{$data->title}</div>";
$content .= "<div class='panel-body'><div style='display:table-row;'><div style='vertical-align:top;display:table-cell'><img src='{$data->media->image->src}' style='max-width:500px;max-height: 400px;' /></div>";
if(isset($data->description) && $data->type != 'photo' && $data->type != 'album' && $data->type != 'cover_photo') $content .= "<div style='vertical-align:top;display:table-cell;padding-left:15px;'>{$data->description}</div>";
if(isset($data->subattachments)) {
$content .= '<div class="fb-subattachments">';
foreach($data->subattachments->data as $attachment) {
$content .= get_fb_attachment($attachment);
}
$content .= '<div style="clear:both;"></div></div>';
}
$content .= '</div></div></div>';
$content .= '</a>';
$content .= "</div>";
return $content;
}
function get_fb_post($data) {
$content = "<div class='panel panel-default fb-post'><div class='panel-body'>";
if(isset($data->from)) {
$content .= "<a href='http://ift.tt/1O8sg9N}' style='font-weight: bold;'>";
if(isset($data->from->picture->data->url)) {
$content .= "<img src='{$data->from->picture->data->url}' style='float:left;width:50px;margin-right:5px;margin-bottom: 5px;' />";
}
if(isset($data->from->name)) {
$content .= $data->from->name;
}
$content .= "</a>&nbsp;";
if(isset($data->story)) {
$story = str_replace($data->from->name,'',$data->story);
$item = end(explode(' ',$story));
$story = str_replace($item,"<a href='{$data->object_id}'>$item</a> ",$story);
$content .= $story;
}
}
if(isset($data->to)) {
$content .= "&nbsp;<span class='mycaret'>&#9654;</span>&nbsp;";
foreach($data->to->data as $to) {
if(isset($first)) {
$content .= "&nbsp;&amp;";
} $first = true;
$content .= "&nbsp;<a href='http://ift.tt/1PFlSKW}' style='font-weight: bold;'>{$to->name}</a>&nbsp;";
}
unset($first);
}
if(isset($data->created_time)) {
$date = get_fb_time($data->created_time);
$content .= "<br /><small style='color: rgb(100,100,100)'>$date</small>";
}
if(isset($data->from->picture->data->url)) {
$content .= '<div style="clear: both;"></div>';
}
if(isset($data->message)) {
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
$message = $data->message;
if(preg_match($reg_exUrl, $message, $url)) {
$message = preg_replace($reg_exUrl, "<a href='{$url[0]}'>{$url[0]}</a> ", $message);
}
$content .= "<p style='margin-bottom: 0px;'>$message</p>";
}
if(isset($data->attachments)) {
foreach($data->attachments->data as $attachment) {
$content .= get_fb_attachment($attachment);
}
}
$content .= '</div>';
if(isset($data->comments)) {
foreach($data->comments->data as $comment) {
$content .= get_fb_post($comment);
}
}
if(isset($data->actions)) {
$content .= "<div style='padding:15px;border-top:1px solid #ddd'>";
$content .= '<iframe src="http://ift.tt/1O8si1y' . $data->id . '&amp;show_faces=false&amp;share=true" scrolling="no" frameborder="0" style="border:none;height:20px;"></iframe>';
$content .= '<a style="float: right;" href=' . $data->actions['like']->link . '>View on Facebook</a>';
$content .= '</div>';
}
$content .= '</div>';
return $content;
}
$content = '';
foreach($graphObject->asArray()['data'] as $data) {
$content .= get_fb_post($data);
}
print $content;
?>
</div>
<style type="text/css">
@media screen {
body * {
vertical-align: top;
}
.fb-subattachments {
margin-top: -15px;
margin-right: -15px;
}
.fb-attachment-container .fb-attachment-container {
padding-right:15px;
max-width:50%;
}
.fb-attachment .fb-attachment img {
max-width: 100%!important;
}
.news-data {
padding-top: 15px;
}
.fb-post .fb-post {
border-radius: 0;
background: #f6f7f8;
border-top: 1px solid #ddd;
margin: 0px;
border-left: 0px;
border-right: 0px;
border-bottom: 0px;
box-shadow: 0;
}
.mycaret {
font-size: 1rem;
display: inline-block;
transform: translateY(15%);
color: rgb(150,150,150);
}
a p, a:link div,
a:visited div,
a:hover div {
color: #000;
background-color: rgba(248,249,250,0.1);
}
a > div:hover {
box-shadow: 0 0 3px 3px rgba(215,220,225,0.1);
}
}
</style>


specific line in question #112



$content .= '<iframe src="http://ift.tt/1O8si1y' . $data->id . '&amp;show_faces=false&amp;share=true" scrolling="no" frameborder="0" style="border:none;height:20px;"></iframe>';


The above code does function unfortunately when i click share it tries to share the "page" that the object is associated with and not the actual object


Aucun commentaire:

Enregistrer un commentaire