My knowledge of Facebook'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 'Please login via Facebook';
}
?>
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.
Related
if someone can help please help it will be appreciated
actually i want to include facebook data in my website
and i am getting an error
Given URL is not allowed by the Application configuration.: One or more of the given URLs is not allowed by the App's settings. It must match the Website URL or Canvas URL, or the domain must be a subdomain of one of the App's domains
since i am new to php
what should be come in place of
api('/me');
$userdata = $facebook->api('/me');
please someone help me i am frustrated
here is the full code
<h1>Facebook SDK Login - Basic Information</h1>
<h4><a href='http://9lessons.info'>9lessons.info</a></h4>
<?php
include('lib/db.php');
require 'lib/facebook.php';
require 'lib/fbconfig.php';
// Connection...
$user = $facebook->getUser();
if ($user)
{
$logoutUrl = $facebook->getLogoutUrl();
try {
$userdata = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
$_SESSION['facebook']=$_SESSION;
$_SESSION['userdata'] = $userdata;
$_SESSION['logout'] = $logoutUrl;
header("Location: home.php");
}
else
{
$loginUrl = $facebook->getLoginUrl(array( 'scope' => 'email,user_birthday'));
echo '<img src="facebook.png" title="Login with Facebook" />';
}
?>
fblogin.php
Go to your Facebook App :-
1. click on "setting" tab(Basic)
App Domains = example.com
site url = http://example.com/
(Advanced) tab
Age Restriction = 13+
2. click on "Status & Review" tab
Do you want to make this app and all its live features available to the general public? = Yes
only these option is required for create an basic app.
You can check for full doc :- sourceaddons or developer facebook
I think you're missing some parameters the API wants. According to https://developers.facebook.com/docs/reference/php/facebook-api/ the API expects the following syntax:
$ret = $facebook->api($path, $method, $params);
So "/me" is the path, but the API does not know what you want to do.
Have a look at the examples like this one here that fetches the user's profile:
https://developers.facebook.com/docs/php/howto/profilewithgraphapi/4.0.0
I have just started mingling with facebook's php sdk, but to no avail. Although php works fine for all the other stuff on my ubuntu instance, facebook's php sdk doesn't (i.e. php code is not executed). All dependencies (only curl and json) are enabled and working.
I tried to look into /var/log/apache2/error.log but it seems that the file is empty - but maybe I'm not looking in the correct place.
So my question is: what would be the steps for me to debug this problem! Thanks for helping a noob!
Here is the boilerplate index.php code for from facebook:
<?php
require '../src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
));
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$statusUrl = $facebook->getLoginStatusUrl();
$loginUrl = $facebook->getLoginUrl();
}
// This call will always work since we are fetching public data.
$naitik = $facebook->api('/naitik');
?>
I am trying to learn some facebook development because it seems people ask for facebook integration one way or another.
I am trying to get something as simple as my name to be echoed on screen using the PHP SDK and I cant seem to do it.
My code is extremely basic, just what's required for the
<?php
require_once('src/facebook.php');
$config = array(
'appId' => '14283923YYYYYYY48',
'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXX',
'allowSignedRequest' => false
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?php
if($user_id) {
$user_profile = $facebook->api('/me','GET');
echo "Name: " . $user_profile['name'];
if (is_null($user_profile))
echo "NULL";
else echo "NOT NULL";
}
?>
</body>
</html>
The name does not show up, and as you can see I tested the $user_profile variable and it comes up NULL, so I assume that the api function doesn't work, doesn't return anything.
Is my code wrong? Or should I look elsewhere for the issue? Any tip or suggestion to solve the issue will be really helpful.
You need to provide a login facility to let the user explicitly allow use of their user ID, and create an access token. This can be done in the PHP SDK or JavaScript SDK (and can be both implemented for dual functionality).
Link:
https://developers.facebook.com/docs/facebook-login/
** Please also view the following: Facebook PHP SDK/Graph API - Returning MY name/info, but not anyone elses?
<?php
if($user_id) {
$user_profile = $facebook->api('/me','GET');
echo "Name: " . $user_profile['name'];
if (is_null($user_profile))
echo "NULL";
else echo "NOT NULL";
}
?>
change the partial section of code above
$user_profile = $facebook->api('/me','GET'); to
$user profile = $facebook->api($user_id, 'GET')
then you would able to get the user profile whose using facebook login in your website
Hope this is help
if you happened to work with a old project in codeigniter 3.x & deployed in aws ELB
in 2018 the facebook call back url mentioned in config or in fb app settings isn't enough,
with ssl, you need to manually edit base_facebook.php and replace redirect_uri from blank to your https url in the function getAccessTokenFromCode() argument,
protected function getAccessTokenFromCode($code, $redirect_uri = 'https://www.example.com/index/fblogin') {
if (empty($code)) {
return false;
}
.....
I was making a facebook application in which i have to show news feeds of the user who is using it. I am using graph API to do this. The problem is that its not getting me feeds.
I used it to show friends like this:
$friends = $facebook->api('/me/friends');
and it is working fine.
For news feeds i use this:
$feeds = $facebook->api('/me/home');
it shows me an error:
Fatal error: Uncaught IDInvalidException: Invalid id: 0 thrown in
/home/content/58/6774358/html/test/src/facebook.php on line 560
when i try to get Profile feed (Wall) by using this:
$feeds = $facebook->api('/me/feed');
it shows me an empty array.
These API calls are showing me results in the graph API page but don't know why not working in my application.Can any one help me please..
My full code is as follows
require_once 'src/facebook.php';
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => 'xxxxx',
'secret' => 'xxxxx',
'cookie' => true,
));
$session = $facebook->getSession();
$fbme = null;
// Session based graph API call.
if (!empty($session)){
$fbme = $facebook->api('/me');
}
if ($fbme) {
$logoutUrl = $facebook->getLogoutUrl();
echo 'Logout';
}else{
$loginUrl = $facebook->getLoginUrl();
echo 'Logout';
}
$friends = $facebook->api('/me/friends?access_token='.$session["access_token"]);
$feeds = $facebook->api('/me/feed?access_token='.$session["access_token"]);
print('<pre>Herere:');print_r($feeds);die;
Did you ask for the read_stream permission during authentication?
What type of Authentication / "Allow" process did you go through?
JavaScript SDK? Facebook PHP SDK? XFBML Login Button?
EDIT- Here are helpful link that will get you started up:
Facebook PHP SDK
Authentication Process
Building Apps on Facebook.com
These are all official docs in Facebook and github.
EDIT: Follow this step by step:
From your original code, look for:
$loginUrl = $facebook->getLoginUrl();
Change it to:
$loginUrl = $facebook->getLoginUrl(array('req_perms'=>'read_stream'));
Uninstall your application first from your account:
http://www.facebook.com/settings/?tab=applications
Then try it again to show new Allow pop-up
EDIT:
It's also about the arrangement of code in the if statements. Use this code:
<?php
require_once 'src/facebook.php';
$session = $facebook->getSession();
$fbme = null;
if($session){
$fbme = $facebook->api('/me');
$friends = $facebook->api('/me/friends');
$feeds = $facebook->api('/me/feed');
$logoutUrl = $facebook->getLogoutUrl();
echo 'Logout';
echo "<pre>".print_r($feeds,TRUE)."</pre>";
}else{
$loginUrl = $facebook->getLoginUrl(array('req_perms'=>'read_stream','canvas'=>1,'fbconnect'=>0));
echo '<script> top.location.href="'.$loginUrl.'"; </script>>';
}
Uninstall your app again from
http://www.facebook.com/settings/?tab=applications
and re-open the application
The Facebook PHP SDK should handle your access token for you, you don't need to append it to your graph API endpoint in you Facebook::api() call.
As #dragonjet pointed out, you need to request the read_stream extended permission from your FB User in order to get access to their feed. Though, the exception you pasted doesn't really match that kind of problem, and your request to /me/home doesn't throw a similar exception (or one about not having access).
I still think this is a permissions issue, so start here for trying to fix it. Here's an example of how to request the appropriate permission.
$facebook = new Facebook(array(
'appId' => FB_APP_ID, //put your FB APP ID here
'secret' => FB_APP_SECRET, //put your FB APP SECRET KEY here
'cookie' => true
));
$session = $facebook->getSession();
if ($session)
{
//check to see if we have friends_birthday permission
$perms = $facebook->api('/me/permissions');
}
//we do this to see if the user is logged & installed
if (empty($session) || empty($perms['read_stream']))
{
//get url to oauth endpoint for install/login
$loginUrl = $facebook->getLoginUrl(array(
//put the URL to this page, relative to the FB canvas
//(apps.facebook.com) here vvv
'next' => 'http://apps.facebook.com/path_to_your_app/index.php',
'req_perms' => 'read_stream'
));
//use javascript to redirect. the oauth endpoint cant be loaded in an
//iframe, so we have to bust out of the iframe and load it in the browser
//and tell the oauth endpoint to come back to the fb canvas location
echo "<script>window.top.location='{$loginUrl}';</script>";
exit;
}
print_r($facebook->api('/me/home'));
print_r($facebook->api('/me/feed'));
I am having some trouble with my little facebook application, I keep getting this friggin error, "Fatal error: Call to undefined method Facebook::require_login()", now the funny bit is that my exact same code is working for other people, but not for me, here is the code.
<?php
require_once( "facebook-php-sdk/src/facebook.php" );
$api_key = "my_api_key";
$secret = "my_secret_key";
$facebook = new Facebook( $api_key, $secret );
$user_id = $facebook->require_login();
echo "Hello World";
echo "Current logged in as <fb:name uid=\"$user_id\" />";
?>
As you can see it is a simple hello USER application, but for some reason this REFUSES to work for me, so if anyone can help out that would be great, thanx in advance!
Are you using the newest version of the PHP sdk?
Facebook::require_login() is a method from the old SDK.
The new SDK (published in conjunction with the Graph API) is not backwards compatible.
The notion of requiring a login doesn't even exist anymore - you just obtain the user's ID as such.
$user_id = $facebook->getUser();
Try out this function:
function appInstance($fbconfig){
try{
include_once "facebook.php";
}
catch(Exception $o){
echo '<pre>';
print_r($o);
echo '</pre>';
}
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true,
));
$session = $facebook->getSession();
$fbme = null;
// Session based graph API call.
if ($session) {
try {
$uid = $facebook->getUser();
$fbme = $facebook->api('/me');
} catch (FacebookApiException $e) {
//d($e); //debug
'Facebook took too long to respond, try again in a few minutes.';
}
}
return $uid;
return $fbme;
return $facebook;
}//end appInstance()
after you make the call to that function use $fbme to pull whatever info you got from your perms like so $firstName=$fbme['first_name'] or $email=$fbme['email'].
a blatant answer, yet, are you on the web domain that you registered your app for on Facebook.
if you registered xxxyy.com, you can use your app only there.
Did you perhaps double check that your domain that is registered with facebook is the same as the dev site you are working on? Probably is, but that one will get you if you don't pay attention. Got me first time around on migration :]