About the like_status with PHP programming - php

When I try to use ['signed_request'] in PHP, I can't get [page][liked] / Page ID value.
I only can get [algorithm], [issued_at], [user], [country], [locale], [age] values from ['signed_request'].
I have created "users_like" Permission in the App's Auth Dialog page. I have set up a Canvas page.
Please help me find out the problem and solution.
P.S. I want to set up a landing page which can check the user whether like it or not.

// create the Facebook Graph SDK object
require_once('facebook.php');
$facebook = new Facebook(array(
'appId'=>'xxxxxxxx', // replace with your value
'secret'=>'xxxxxxxx' // replace with your value
));
$signedRequest = $facebook->getSignedRequest();
// Inspect the signed request
if($signedRequest['page']['liked'] == 1){ //Do your stuff here }
That is all that's required, you don't need any permissions.
EDIT: Also, make sure that you are actually trying to access a page, not, for example, an app canvas.

Related

I can't get the session of user facebook, getUser return 0 - codeigniter

Regards,
I have a problem with Facebook SDK-PHP and Codeigniter.
I do not get the user's session when I try to capture and obtain basic data for that user.
The error I get is: "An active access token must be used to query information about the current user."
And when I apply the "getUser ()", I get "0".
NOTE: I download the php-sdk and put on my directory libraries the files base_facebook.php and facebook.php
In my application/config/config.php I put the key and app_secret of my facebook application.
Config.php :
$config['base_url'] = 'http://localhost/myapp_local';
$config['app_id'] = 'xxxxxxxxxxxxxx';
$config['secret'] = 'xxxxxxxxxxxxxxxxxxxxxxxx';
$config['app_name'] = 'http://apps.facebook.com/myapp_local/';
In my Controller Main.php i have the follow code:
class Main extends CI_Controller {
public function __construct() {
parent::__construct();
$fb_config = array(
'appId' => $this->config->item('app_id'),
'secret' => $this->config->item('secret')
);
$this->load->library('facebook', $fb_config);
private function _getInfoUserFB() {
try {
$fb_id = $this->facebook->getUser();
if ($fb_id) {
$fb_data = $this->facebook->api('/me');
return $fb_data;
}
} catch (Exception $exc) {
echo $exc;
}
}
[....]
}
Now, in my variables $fb_id , where obtain the bool value, always i have "0".
So, when i try to get the data of the current user facebook always i have a Exception with the message: "An active access token must be used to query information about the current user."
$fb_data['id'] = NULL
$fb_data['first_name'] = NULL
$fb_data['last_name'] = NULL
What i doing wrong here? Any step or setting I miss?
NOTE 2: I don't have some button Login with Facebook, my app connect directly to facebook via the configuration of my app on facebook. (Canvas URL, Secure Canvas URL, Canvas Page, Site URL)
Note 3: My application have some permission that need require, So I put the necessary permissions on Settings-Permissions BUT don't show me the option of "Auth Referrals" to show Dialog confirm permissions.
I use CodeIgniter version 2.1.3 and PHP-SDK v 3.2.0
You have to go to Facebook Developer Site -> Apps -> Permission. And set the basic info permission for your app.
Then in your function getLoginUrl(array("scope" => "user_about_me))), you have to give the parameters.
Finally you have to save the access token $this->facebook->getAccessToken();
If you have problems you can ask me. I have setup my Codeigniter with nearly all Social Media Platforms :)
Facebook does really bad with localhost. I've experienced this several times too. The solution I've found, which lets me develop locally, is to edit my hosts file (C:\Windows\system32\drivers\etc\hosts or /etc/hosts), and add something like the following line in there:
127.0.0.1 myfbapp.local
Using this, if you point your browser to http://myfbapp.local/, it will actually go to 127.0.0.1 and that means localhost. Of course, you would have to update your CI config too, and change your base URL to http://myfbapp.local.
As far as I can tell, the Facebook PHP SDK is having trouble setting cookies if there is no dot (.) in the domain name, so that's exactly what my solution does.
OK!, i resolved it. The fixed was the interaction with the newer version have to this form:
http://www.galalaly.me/index.php/2012/04/using-facebook-php-sdk-3-with-codeigniter-2-1/
And then, you must be call the function that obtain the information of current user for first time when your app load for first time. In other words, you call the function in the MAIN, INDEX view of your website.
If you call in other time, your function can't obtain the access_token of current user.

facebook app redirected from website

I have made a facebook application
I have uploaded all my code on my server, so facebook can retrieve it from there.
All my code is in HTML, php and javascript.
When the user visits www.mywebsite.com/facebook/app they will go to my index.php file.
But if the user types in www.mywebsite.com/facebook/app/pictures/picture.jpg he will see the picture.
Now I want to make sure, this content only can be access through facebook.
So I want to redirect everybody who tries to enter www.mywebsite.com/facebook/app/..../ to my facebook application www.facebook.com/myapp
Is there any way to do this?
Thank you
While it is very unreliable you could use the $_SERVER['HTTP_REFERER'] variable in php to see if the user typed in the url directly in their browser. It will be empty if the user has typed in the url directly, but i believe it should be set if your page is embedded through facebook.
see http://www.electrictoolbox.com/php-http-referer-variable/ for more info
How I fixed it
// Saves the original URL
$Origin_URL = $_SERVER['HTTP_REFERER'];
// The original URL must also contain the word facebook
$face = "facebook";
// Checks if the URL contains 'facebook'
$contains = strpos($Origin_URL,$face);
// If the original url is empty or doesn't contain facebook
// the user will be redirected to the facebook site
if(Origin_URL == "" || $contains === false)
header("Location: www.facebook.com/website");
This is not bulletproof, so try toy with it a little :)

getLoginURL not including client_id parameter

I'm trying to implement a FB login system on my site. I'm starting with the most simple test login page I can and hoping to build from there.
When I use the $facebook->getLoginUrl() method, the URL generated produces an error every time I click on it.
I noticed that the URL produced by getLoginUrl() does not include the client_id parameter in the URL string. There doesn't seem to be anything in the docs about having to set that client_id in the getLoginUrl call. I assume it's pulled from a property of the Facebook object.
Here's my code and what it outputs.
require_once("php-sdk/facebook.php");
$config = array();
$config[‘appId’] = /* my app id string */;
$config[‘secret’] = /* my app secret string */;
$config[‘fileUpload’] = false; // optional
$facebook = new Facebook($config);
$login_url = $facebook->getLoginUrl(array("scope"=>"email"));
echo "Login with Facebook";
Obviously the appID and secret are the actual values. I've quadruple checked to make sure they are accurate.
The (edited) login_url generated is:
https://www.facebook.com/dialog/oauth?redirect_uri=[mysite]&state=[state_str]&scope=email
It should be:
https://www.facebook.com/dialog/oauth?client_id=[appID]&redirect_uri=[mysite]&state=[state_str]&scope=email
When I edit the URL in the browser to include client_id it works. Obviously I could manually create the correct login url myself each time, but I want to see if there's something wrong with the way I'm using Facebook::getLoginUrl()
Just realized when I saw the code pasted in here that when I copied off the Facebook documentation, it copied what should be single-quotes as smart-quotes. Thus the facebook object was not being properly initialized with the app ID and api secret. Changing them to single quotes resolved the issue.

Is it possible when developing a facebook app to remove permissions and start again? I need to change my scope and keep getting errors

I am new to this Facebook coding game so have stumbled upon a fault that I think only I may be getting. I initially wanted to allow the user logging in to manage their pages, however as an after thought, the client wanted their users to be able to post to their individual pages as well.
So, I added the publish_stream permission to the scope, but am stuck with just getting Facebook has an error.
Do i have to deactivate myself from the app first a la the point on the documentation:
https://developers.facebook.com/docs/authentication/
Code attached, any help would be great:
$facebook = new Facebook(array(
'appId' => $this->app_id,
'secret' => $this->secret,
));
$s = setting::findAppSettingSpecific('social','facebook');
// exit;
$facebook->setAccessToken($s);
$user = $facebook->getUser();
$dia_url = 'http://localhost/social/accept/';
$login_url = $facebook->getLoginUrl($params = array('redirect_uri' => $dia_url,'scope' => 'manage_pages,publish_stream'));
if($user == 0) {
}
else
{
echo ("<script> top.location.href='".$login_url."'</script>");
echo 'Facebook Loves Us';
return true;
}
It is working on Localhost, just in case someone points that out, however only started calling errors when adding an extra permission.
Thanks in Advance.
You have a few options:
(1) Use the Graph Explorer, select your app on the top right select box, change the method to DELETE and in the path put me/permissions.
This will remove all of the permissions you granted the app.
(2) If the app is bookmarked then in your facebook homepage hover the app name on the left bookmarks panel, on it's left an edit icon appears, click that and then Edit Settings, there you can remove some individual permissions.
(3) In facebook on the top right click the arrow that faces down, in the menu click *Privacy Settings" and in that page click the Edit Settings of "Apps and Websites" there you can control the apps you use.
Edit
(4) You can remove individual permissions with a DELETE request to /me/permissions with an extra parameter of permissions=publish_stream for example
Thanks to #Igy who pointed out the 4th option.

Redirect to facebook app through app-website

I have made an app as a Page Tab. I need to redirect to the facebook-app if someone opens the website url directly. For that, I need to check the content of the address bar and if it doesn't contain www.facebook.com inside it, I will redirect to the facebook-app url. But, the problem is that inside the facebook iframe I am unable to get the content of the address bar.
Can you please tell me a good way to enable redirect to the fb-app in case someone accesses my website url directly?
Probably the easiest way to achieve this, is to do next check in JavaScript:
if (window.top !== window){
window.location = 'http://facebook.com/YOUR_PAGE?sk=v_YOUR_APP_ID'
}
If you are wanting to redirect to a tab, a more complete way of doing this is checking for the 'page' parameter that is only sent through in the signed request for a page tab.
The reason being that only checking for the signed request will allow users to access your 520px tab app in a Facebook canvas (i.e. https://apps.facebook.com/yourapp which may not be what you want.
So for example
$fb = new Facebook( array(
'appId' => <your_app_id>,
'secret' => <your_app_secret>);
$sr = $fb -> getSignedRequest();
And then redirect the user as they hit the page
if (!($sr['page'])) {
die('<script>window.top.location = "<your_tab_url>"</script>');
};
Facebook posts a signed request parameter when you open an app in fanpage.
You can check if that parameter is null or not.
In php,
if(isset($_REQUEST["signed_request"]){
// Opened in facebook
}else{
//opened outside facebook.
}
In asp.net
if(Request.Form["signed_request"] !=null)
{
// Opened in facebook
}
else
{
// opened outside facebook.
}

Categories