I'm using this code:
<div class="fb-like" data-href="https://web.facebook.com/mykliktransport/" data-layout="box_count" data-action="like" data-size="large" data-show-faces="false" data-share="false"></div>
it is showing like this:
but what is want is only the count to show it here :
Its working now... This may help
Get your token number from https://developers.facebook.com/docs/graph-api
<?
$fb_page = '1907481709532899';
$access_token = 'Token Number';
$url = "https://graph.facebook.com/v2.2/".$fb_page.'?access_token='.$access_token;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
curl_close($curl);
$details = json_decode($result,true);
echo $details['likes'];
?>
Related
I have a few websites that have a custom Facebook feed which I load in using the Graph API.
In the past everything loaded very quick but since about a week or two all sites stop loading at the feed part and only finishes loading completely after 30 seconds or more.
What can be the cause of this?
I am sure it is the Facebook feed because when I remove it the site loads quickly again.
Below is an example feed from a website which has this issue:
$json_object = file_get_contents("https://graph.facebook.com/v3.2/thepagesid/posts?fields=full_picture%2Cmessage%2Cstory%2Cpermalink_url%2Cupdated_time%2Cfrom&access_token=mytoken");
$feedarray = json_decode($json_object);
$f = 0;
foreach ( $feedarray->data as $key => $feed_data )
{
if($feed_data->full_picture != ''){
$fbimage = $feed_data->full_picture;
}else{
$fbimage = 'assets/images/fbnoimg.jpg';
}
$shortstrfb = substr($feed_data->message, 0, 170) . '...';
if($feed_data->message != ''){
$f++;
}
if($f > 4){
break;
}
if($feed_data->message != '' && $feed_data->from->name == 'facebook page name'){
$facebookfeed .= '
<div class="col-lg-3 col-md-4 col-sm-6">
<div class="single-product-wrap fbwrapdiv">
<div class="product-image">
<a href="'.$feed_data->permalink_url.'" target="_blank">
<span class="datefb">'.date("d-m-Y",strtotime($feed_data->updated_time)).'</span>
<img class="fbimgclass" src="'.$fbimage.'" alt=""></a>
<div class="product-action">
<i class="fab fa-facebook-f"></i>
</div>
</div>
<div class="product-content">
<div class="price-box">
<p class="facebooktext">'.$shortstrfb.'</p>
</div>
</div>
</div>
</div>';
}
}
echo $facebookfeed;
I fixed it by using curl.
I replaced:
$json_object = file_get_contents("https://graph.facebook.com/v3.2/thepagesid/posts?fields=full_picture%2Cmessage%2Cstory%2Cpermalink_url%2Cupdated_time%2Cfrom&access_token=mytoken");
$feedarray = json_decode($json_object);
With:
$graph_url = 'https://graph.facebook.com/v3.2/thepagesid/posts?fields=full_picture%2Cmessage%2Cstory%2Cpermalink_url%2Cupdated_time%2Cfrom&access_token=mytoken';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $graph_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$output = curl_exec($ch);
curl_close($ch);
$feedarray = json_decode($output);
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
PHP SCRIPT issue with passing the uploaded image to facebook->api function
I have this code and its working awsomely fine just and just only one problem i am not able to pass my uploaded image to facebook->api (whether its valid or not) and the following always echo
echo 'Only jpg, png and gif image types are supported!';
i even remove the check on image type and try to get the image from
$img = realpath($_FILES["pic"]["tmp_name"]);
but $img gets nothing in it and uploads a by default empty image on facebook as my page
Kindly check my following code and let me know what should is wrong with my code so i'll become able to upload images
<?
require 'src/facebook.php';
$app_id = "364900470214655";
$app_secret = "xxxxxxxx";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true,
'fileUpload' => true,
));
$user = $facebook->getUser();
//echo $user;
if(($facebook->getUser())==0)
{
header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos,offline_access,manage_pages'))}");
exit;
}
else {
$accounts_list = $facebook->api('/me/accounts');
echo "i am connected";
}
$valid_files = array('image/jpeg', 'image/png', 'image/gif');
//to get the page access token to post as a page
foreach($accounts_list['data'] as $account){
if($account['id'] == 194458563914948){ // my page id =123456789
$access_token = $account['access_token'];
echo "<p>Page Access Token: $access_token</p>";
}
}
//posting to the page wall
if (isset($_FILES) && !empty($_FILES))
{
if( !in_array($_FILES['pic']['type'], $valid_files ) )
{
echo 'Only jpg, png and gif image types are supported!';
}
else{
#Upload photo here
$img = realpath($_FILES["pic"]["tmp_name"]);
$attachment = array('message' => 'this is my message',
'access_token' => $access_token,
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'example.org',
'description' => 'this is a description',
'picture' => '#' . $img,
'actions' => array(array('name' => 'Get Search',
'link' => 'http://www.google.com'))
);
$status = $facebook->api('/194458563914948/feed', 'POST', $attachment); // my page id =123456789
var_dump($status);
}
}
?>
<body>
<!-- Form for uploading the photo -->
<div class="main">
<p>Select a photo to upload on Facebook Fan Page</p>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<p>Select the image: <input type="file" name="pic" /></p>
<p><input class="post_but" type="submit" value="Upload to my album" /></p>
</form>
</div>
</body>
Creating a New Album and Adding a Photo
This is the scenario where you upload a photo to the ALBUM_ID/photos Graph API endpoint. The user interface for this example allows the user to select a photo and add a caption before submitting the new photo.
Using PHP:
<?php
$app_id = "YOUR_APP_ID";
$app_secret = "YOUR_APP_SECRET";
$post_login_url = "YOUR_POST-LOGIN_URL";
$album_name = 'YOUR_ALBUM_NAME';
$album_description = 'YOUR_ALBUM_DESCRIPTION';
$code = $_REQUEST["code"];
//Obtain the access_token with publish_stream permission
if(empty($code))
{
$dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode($post_login_url)
. "&scope=publish_stream";
echo("<script>top.location.href='" . $dialog_url .
"'</script>");
}
else {
$token_url= "https://graph.facebook.com/oauth/"
. "access_token?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token'];
// Create a new album
$graph_url = "https://graph.facebook.com/me/albums?"
. "access_token=". $access_token;
$postdata = http_build_query(
array(
'name' => $album_name,
'message' => $album_description
)
);
$opts = array('http' =>
array(
'method'=> 'POST',
'header'=>
'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = json_decode(file_get_contents($graph_url, false,
$context));
// Get the new album ID
$album_id = $result->id;
//Show photo upload form and post to the Graph URL
$graph_url = "https://graph.facebook.com/". $album_id
. "/photos?access_token=" . $access_token;
echo '<html><body>';
echo '<form enctype="multipart/form-data" action="'
.$graph_url. ' "method="POST">';
echo 'Adding photo to album: ' . $album_name .'<br/><br/>';
echo 'Please choose a photo: ';
echo '<input name="source" type="file"><br/><br/>';
echo 'Say something about this photo: ';
echo '<input name="message" type="text"
value=""><br/><br/>';
echo '<input type="submit" value="Upload" /><br/>';
echo '</form>';
echo '</body></html>';
}
?>
<?php
define('YOUR_APP_ID', 'x');
define('YOUR_APP_SECRET', 'x');
function get_facebook_cookie($app_id, $app_secret) {
$args = array();
parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
ksort($args);
$payload = '';
foreach ($args as $key => $value) {
if ($key != 'sig') {
$payload .= $key . '=' . $value;
}
}
if (md5($payload . $app_secret) != $args['sig']) {
return null;
}
return $args;
}
$cookie = get_facebook_cookie(YOUR_APP_ID, YOUR_APP_SECRET);
$url = 'https://graph.facebook.com/me?access_token=' . $access_token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
$user = json_decode($response);
print_r($user);
?>
<html>
<body>
<?php if ($cookie) { ?>
Welcome <?php ?>
<?php } else { ?>
<fb:login-button></fb:login-button>
<?php } ?>
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({appId: '<?= YOUR_APP_ID ?>', status: true,
cookie: true, xfbml: true});
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
</script>
</body>
</html>
Does anyone know why I am getting this exception? print_r of $user is yielding
stdClass Object ( [error] => stdClass Object ( [type] => OAuthException [message] => An active access token must be used to query information about the current user. ) )
Why is this happening?
well for one thing $access_token isn't set before it's used?
i'm trying to prepare an application for Facebook. application software is created. "localhost" tests without any problems but links within the Facebook application is not working... e.g. "page.php?page=test"... application, using the fan page. the first page is opened, UserID and PageID data is seamlessly but i click the link to the sample, the data is blank. i tried to target="_top" but not work...
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
'cookie' => false,
));
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
$m2_facebook_kullanici_id = $data['user_id'];
$m2_facebook_sayfa_id = $data['page']['id'];
if(empty($_GET['sayfa'])){
...working here USERID and PAGEID...
}
if($_GET['sayfa'] == 'guncelle'){
...does not work here USERID and PAGEID...
}
i wonder what's wrong?
thank you...
basic solved:
<?php
require 'facebook.php';
$signed_request = $_REQUEST["signed_request"];
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
'cookie' => false,
));
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
$m2_facebook_kullanici_id = $data['user_id'];
$m2_facebook_sayfa_id = $data['page']['id'];
setcookie("facebook_user_id", $m2_facebook_kullanici_id);
setcookie("facebook_page_id", $m2_facebook_sayfa_id);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Title</title>
</head>
<body>
<?php
if(empty($_GET['sayfa'])){
echo 'Güncelle<br />';
echo '<h3>Home Page</h3>';
echo '<strong>User ID:</strong> ' . $m2_facebook_kullanici_id . '<br />';
echo '<strong>Page ID:</strong> ' . $m2_facebook_sayfa_id;
}
if($_GET['sayfa'] == 'guncelle'){
$m2_facebook_kullanici_id = $_COOKIE["facebook_user_id"];
$m2_facebook_sayfa_id = $_COOKIE["facebook_page_id"];
echo 'Back<br />';
echo '<h3>Settings Page</h3>';
echo '<strong>User ID:</strong> ' . $m2_facebook_kullanici_id . '<br />';
echo '<strong>Page ID:</strong> ' . $m2_facebook_sayfa_id;
}
?>
</body>
</html>
SOLUTION:
just add the following code to the end of links
?signed_request=' . $_REQUEST['signed_request'] . '
or
&signed_request=' . $_REQUEST['signed_request'] . '
a simple example of the application
<?php
require 'facebook.php';
function parse_signed_request($signed_request, $secret) {
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$sig = base64_url_decode($encoded_sig);
$data = json_decode(base64_url_decode($payload), true);
if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
error_log('Unknown algorithm. Expected HMAC-SHA256');
return null;
}
$expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
if ($sig !== $expected_sig) {
error_log('Bad Signed JSON signature!');
return null;
}
return $data;
}
function base64_url_decode($input) {
return base64_decode(strtr($input, '-_', '+/'));
}
$data = parse_signed_request($_REQUEST['signed_request'], 'a5aaf146542d50c11f55e001c0fb8f31');
$m2_facebook_kullanici_id = $data['user_id'];
$m2_facebook_sayfa_id = $data['page']['id'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Title</title>
</head>
<body>
<?php
if(empty($_GET['sayfa'])){
echo 'Settings<br />';
echo '<h3>Home</h3>';
echo '<strong>User ID:</strong> ' . $m2_facebook_kullanici_id . '<br />';
echo '<strong>Page ID:</strong> ' . $m2_facebook_sayfa_id;
}
if($_GET['sayfa'] == 'guncelle'){
echo 'Home<br />';
echo '<h3>Settings</h3>';
echo '<strong>User ID:</strong> ' . $m2_facebook_kullanici_id . '<br />';
echo '<strong>Page ID:</strong> ' . $m2_facebook_sayfa_id;
}
?>
</body>
</html>
special thank: moguzalp and Mesut Eyrice
<?php
include_once 'config.inc.php';
function get_facebook_cookie($app_id, $app_secret) {
$args = array();
parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
ksort($args);
$payload = '';
foreach ($args as $key => $value) {
if ($key != 'sig') {
$payload .= $key . '=' . $value;
}
}
if (md5($payload . $app_secret) != $args['sig']) {
return null;
}
return $args;
}
$cookie = get_facebook_cookie(174346302604624, be273568bf3e3b6c194fdcc09448201d);
//catch the exception that gets thrown if the cookie has an invalid session_key in it
$user = json_decode(file_get_contents(
'https://graph.facebook.com/me?access_token=' .
$cookie['access_token']));
?>
//above is MY facebook script
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
above the code is in my head
<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/XdCommReceiver.js"
type="text/javascript"></script>
above code is in body tag
<div><fb:profile-pic uid='<?php echo $user->id; ?>' ></fb:profile-pic></div>
<?php } ?>
<div id="fb-root"></div>
<script>
FB.init({appId: '<?= 174346302604624 ?>',status: true,
cookie: true, xfbml: true});
FB.Event.subscribe('auth.login',function(response) {
window.location.reload();
});
</script>
above code is just before </body> tag can any body tell me what is fault,i want to show profile pic
for fbml tags in iframe app you can try this one
<fb:serverFbml>
<script type="text/fbml">
<fb:fbml>
<div><fb:profile-pic uid=""></fb:profile-pic></div>
</fb:fbml>
</script>
</fb:serverFbml>