Facebook sdk using php: how to get theuserid of the connected user - php

how can i get the current logged in userid ?
im pretty sure my appId is correct
$facebook = new Facebook(array(
'appId' => '...',
'secret' => '...',
));
im using this method but i returns 0;
$facebook->getUser();

If you want to get facebook user id with this code
$facebook = new Facebook(array(
'appId' => 'your_app_id',
'secret' => 'your_app_secret',
));
$user = $facebook->getUser();
You have condition to should have done before, the first condition is you put that code in the page that redirected after the user login via fb provide by this code
$params = array(
'display'=>'popup',
'redirect_uri' => 'http://example.com'
);
$loginUrl = $facebook->getLoginUrl($params);
OR the second condition is you already have the user 'Access Token', so the code will be like this
$facebook = new Facebook(array(
'appId' => 'your_app_id',
'secret' => 'your_app_secret',
));
$facebook->setAccessToken('user_access_token');
$user = $facebook->getUser();
If you don't clear about the Facebook Login flow using PHP SDK, you can visit this page

$myData=$facebook->api('/me');
$myid=myData['id'];
$myName=myData['name'];
$myUsername=myData['username'];
[id] => your id
[name] => your full name
[first_name] => your firs name
[last_name] => lastname
[link] => profil link
[username] => username
[gender] => your gender (male,female)
[email] => your mail (if have email permission)
[timezone] => 2
[locale] => your locale
[verified] => 1

Follow this guide: Login for Server-side Apps. Basically, you have to generate a login URL, receive a code generated by Facebook, and with this code, request for an access token.

Related

Facebook graph: Post picture as page not as user

I'm using FB App to publish posts to my FB pages.
So when i use this code for links, it works.
$facebook = new Facebook(array(
'appId' => $fb_app_id,
'secret' => $fb_secret,
'cookie' => true
));
$attachment = array(
'access_token' => $row["token"],
'name' => $_POST["name"],
'picture' => $_POST["picture"],
'message' => $_POST["message"],
'link' => $_POST["link"]);
try
{
$facebook->api("/".$row['id']."/feed", "POST", $attachment);
}
catch(Exception $e)
{
echo('exception caught '.$e->getMessage());
}
But, when i want to share pictures as a page:
$facebook = new Facebook(array(
'appId' => $fb_app_id,
'secret' => $fb_secret,
'cookie' => true,
'fileUpload' => true,
'allowSignedRequest' => false
));
$attachment = array('access_token' => $row["token"], 'url' => 'http://image.link.jpg', 'message' => $_POST["message"]);
try
{
$facebook->api("/".$row['id']."/photos", "POST", $attachment);
}
catch(Exception $e)
{
echo('exception caught '.$e->getMessage());
}
The app post picture as user. USER to PAGE.
So i found that i need to use publish_pages permission, but my access token contains both publish_actions and publish_pages. I can't change that because I'm using one access token for a lot of things.
So i have to ask, is there a way to explicitly choose what permissions i want to use from access token. FB API have same code for page and user picture publish /user_id/photos and /page_id/photos.
Need to use Page access token, there's no other way.

facebook send notification to users

i want to send notification who access my app.i use following code.
but it not work.i was goggling about it.but every example looks like my one.but it not work for me.
here is my code
require 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'myappid',
'secret' => 'myappsecret',
));
$app_id='myappid';
$app_secret='myappsecret';
$app_access_token=$app_id.'|'.$app_secret;
$user_id='359211054272153';
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$response = $facebook->api( '/359211054272153/notifications', 'POST/v2.2/', array(
'template' => 'You have received a new message.',
'href' => 'RELATIVE URL',
'access_token' => $app_access_token
) );
i am used old version of Facebook sdk it want to real user id.
but currently
Facebook gives app scoped userid. thats the error.
using new sdk v4 it not problem.

Post to Facebook with PHP

I have to post on a facebook page, the titles of articles (with link) posted on a blog.
Unfortunately I can only post the link, but without the title. How can I do?
The PHP code I am using is this:
require ("facebook.php");
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXXXXX',
'cookie' => true
));
// id page
$pageId = "136860079786056";
// permanent session token
$permSess = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
// writing
$test = $facebook->api(array(
"uid" => $pageId,
"method" => "stream.publish",
"access_token" => $permSess,
"message" => "http://www.mysite.it/functions.php?p=view&id=31", ));
There is a link variable you can use for the link.
Eg
"link" => "http://google.com",
"message" => "The search engine we can't mention"

Facebook not sending get parameter after login success

I am using php sdk for my facebook application.
I am using following code for login url
if (isset($username)) {
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => $scope,
'redirect_uri' => 'http://www.mysite.com/redirect.php?username=' . $username
)
My problem is that after login when facebook redirects page to redirect.php but it removes ?username=ursername.
I need the username as get parameter after login.
How can i do this. Please suggest me.
Thanks in advance.
You cannot pass username to the redirect_uri. It's not really meant for this, but there is a state parameter you can pass that gets returned to you.
See the documentation:
https://developers.facebook.com/docs/reference/dialogs/oauth/
define('REDIRECT_URI',"YOUR-REDIRECT-URL-HERE");
$user = null;
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET,
'cookie' => true
));
if (isset($username)) {
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => $scope,
'redirect_uri' => REDIRECT_URI
)

how to create a score in facebook application?

from facebook docs it says:
"You can post a score or a user by issuing an HTTP POST request to /USER_ID/scores with the app access_token as long as you have the publish_actions permission."
well i have publish_actions permission and i'm trying the following code but it doesn't work...
require('php-sdk/src/facebook.php');
$facebook = new Facebook(array(
'appId' => '123546321654654654654',
'secret' => 'd9132156afasdfadsfsdfewfg79f5f16d0',
'cookie' => true
));
$attachment = array('score' => 1005);
$postscore = $facebook->api('/12345612/score/','POST',$attachment);
You need to send your app access token in the attachment array. So simply update your array attachment to
$attachment = array( 'score' => 1005, 'access_token' => $facebook->getAppId().'|'.$facebook->getApiSecret() )
$postscore = $facebook->api('/12345612/score/','POST',$attachment);
I'm not familiar with php but link for posting the score is "/USER_ID/scores/". You have forgotten one 's' in the end of the scores.

Categories