Facebook Like button using PHP
My knowledge of Faceb开发者_JAVA百科ook's PHP SDK is limited at this point but is it possible to use their PHP SDK to create a text link version of their 'Like' button?
No, I'm afraid this is not possible.
I am currently using the Like button as well as the like box on our Website but the JS SDK has a kind of high execution time and takes a while to load and thus I am currently looking for a proper way to avoid the JS SDK.
The Like button can - more or less - be replaced with the Graph API whereas the like box can't.
To set a like for an object (website, photo, link, whatever) you can simply call the graph api with the method=post Parameter.
<?php
require_once("facebook.php"); //from the FB SDK
$config = array();
$config['appId'] = 'your_app_id';
$config['secret'] = 'your_app_secret';
$facebook = new Facebook($config);
$user = $facebook->getUser();
if(!$user){
$loginUrl = $facebook->getLoginUrl(array('scope'=>'publish_stream, user_likes', 'redirect_uri'=>'www.example.com'));
}
if($user){
try{
$user_profile = $facebook->api('/me');
$access_token = $facebook->getAccessToken();
$access_token = $facebook->getAccessToken();
$attend = "https://graph.facebook.com/www.example.com/like?method=post&access_token=".$access_token;
file($attend);
}
catch(FacebookApiException $e){
error_log($e);
$user = NULL;
}
}
else{
echo '<a href="'.$loginUrl.'">Please login via Facebook</a>';
}
?>
You'll need an FB Application though, as you have to specify the app_id and app_secret for the PHP SDK, which you don't have to when you are using just the like button.
There's a /likes graph API object that the docs say you can write to with the proper permissions. I've never tried it though.
精彩评论