How to authorising my PHP code to publish on Facebook? - php

I want to publish some test wall post on my Facebook USER account wall (Not any Facebook Page Or Facebook App just normal user account). I tried Facebook SDK like below but nothing happened it just asked me to login. but i want to login programmatically please tell me how to do this and post to my own wall?
require_once "API_Library/Facebook/src/facebook.php";
$config = array(
'userId' => 'USER_ID',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
if($user_id)
{
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try
{
$user = $config['userId'];
$user_profile = $facebook->api('/me', 'GET');
echo "Name: " . $user_profile['name'];
$ret_obj = $facebook->api('/me/feed', 'GET', array(
'link' => "https://www.facebook.com/$user",
'message' => 'Posting with the PHP SDK!'
));
echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
}
catch(FacebookApiException $e)
{
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
}
else
{
// No user, print a link for the user to login
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
}

You are doing an HTTP GET instead of a HTTP POST at
$ret_obj = $facebook->api('/me/feed', 'GET', array(
'link' => "https://www.facebook.com/$user",
'message' => 'Posting with the PHP SDK!'
));
You might want to change the API method to POST instead.

Related

Verify/validate User access for android app using facebook SDK at server side in PHP?

I have a android app. User can access to the app using facebook login. Once user logged in, his all data/activity will push to web server using php.
I just wanted to validate user access for app while he accessing the app.
Just wanted to check user app access status using his access token when his request comes to php web server.
The main aim is to deny fake users request from android to web server
for the security purpose.
Can anyone help me to sort out my problem .
Thanks in advance..
<?php
try{
require_once(dirname(__FILE__) . '\facebook-php-sdk\src\Facebook\facebook.php' );
}
catch(Exception $o){
print_r($o);
}
$config = array(
'appId' => '123456',
'secret' => '5ca2b11ea4c8sdsdsd',
'allowSignedRequest' => false // optional but should be set to false for non-canvas apps
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$user_id = '123456';
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$ret_obj = $facebook->api('/'.$user_id.'/feed', 'POST',
array(
'link' => 'www.example.com',
'message' => 'Posting with the PHP SDK!'
));
// $ret_obj = $facebook->api('/me/feed', 'POST',
// array(
// 'access_token' => $facebook->getAccessToken(),
// 'link' => 'www.example.com',
// 'message' => 'Posting with the PHP SDK!'
// ));
echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
// Give the user a logout link
echo '<br />logout';
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
echo "<pre>";var_dump($e);
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream'
));
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
// No user, so print a link for the user to login
// To post to a user's wall, we need publish_stream permission
// We'll use the current URL as the redirect_uri, so we don't
// need to specify it here.
$login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) );
echo 'Please login.';
}?>

PHP Facebook SDK

Hi i need help with a small error i received
Warning: Illegal string offset 'name' in C:\xampp\htdocs\facebooktest.php on line 41
You are logged into centralrp using your facebook account! [1] when im tring to show the name
<?php
// Remember to copy files from the SDK's src/ directory to a
// directory in your application on the server, such as php-sdk/
require_once('php-sdk/facebook.php');
$config = array(
'appId' => '*',
'secret' => '*',
'allowSignedRequest' => false // optional but should be set to false for noncanvas apps
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?php
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
if (!isset($_SESSION['user']['facebookposted']))
{
$ret_obj = $facebook->api('/me/feed', 'POST',
array(
'link' => 'app.com',
'message' => 'Just logged into App with Facebook.'
));
echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
$_SESSION['user']['facebookposted'] = true;
//ADD THE LOGIN STUFF HERE LATER
}
else
{
$_SESSON['user']['loggedin'] = true;
echo 'You are logged into centralrp using your facebook account! ['.$user_id['name'].']';
}
// Give the user a logout link
echo '<br />logout';
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream'
));
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
// No user, so print a link for the user to login
// To post to a user's wall, we need publish_stream permission
// We'll use the current URL as the redirect_uri, so we don't
// need to specify it here.
$login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) );
echo 'Please login.';
}
?>
There is no name key in $user_id because $user_id is not an array.
if you need the name, get it like this:
try {
$user_info = $facebook->api('/me/');
echo '<pre>Name: ' . $user_info['name'] . '</pre>';
} catch(FacebookApiException $e) {
echo 'There was an error';
}

Facebook 0Auth Client login not working. PHP APP

I have created a facebook app using PHP-SDK which posts a message on user's feed and it will print app access token on the canvas page. It does not opens any Dialog box for permissions access to app, also after opening the app it does not do anything. It just show "Please Login" where login isa button having some link which too isnot working. Help me!
<?php
// Remember to copy files from the SDK's src/ directory to a
// directory in your application on the server, such as php-sdk/
require_once('src/facebook.php');
$config = array(
'appId' => 'myappidhere',
'secret' => 'mysecrete,
'allowSignedRequest' => false // optional but should be set to false for non-canvas apps
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?php
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$ret_obj = $facebook->api('/me/feed', 'POST',
array(
'link' => 'www.facebook.com',
'message' => 'Posting with the PHP SDK!Learning PHP and Facebook API :D :D'
));
echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
$token = $facebook->getAccessToken();
echo 'test';
echo $token;
// Give the user a logout link
echo '<br />logout';
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream'
));
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
// No user, so print a link for the user to login
// To post to a user's wall, we need publish_stream permission
// We'll use the current URL as the redirect_uri, so we don't
// need to specify it here.
$login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) );
echo 'Please login.';
}
?>
Is there any special settings which i have to do in my APP dashboard on facebook?
You missing single quotes at:
'secret' => 'mysecrete',
Is this a typo only in this post, not in your code? And is any arrors displayed? Use var_dump for dumping url:
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream'
));
var_dump($login_url);

How to publish a post to all my app users?

I'm trying to post to all my app user wall
& and i use this code to post to only one user : ==>>
this code to post in only one wall
<?
$config = array(
'appId' => 'xxxxxxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxx',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$ret_obj = $facebook->api('/me/feed', 'POST',
array(
'link' => 'www.example.com',
'message' => 'Posting with the PHP SDK!'
));
echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream'
));
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
// Give the user a logout link
echo '<br />logout';
} else {
// No user, so print a link for the user to login
// To post to a user's wall, we need publish_stream permission
// We'll use the current URL as the redirect_uri, so we don't
// need to specify it here.
$login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) );
echo 'Please login.';
}
?>
</body>
</html>
How to publish a post to all my app users ?!!
please advice !
Thanks
Get all the app users-
With FQL, just do :
$fql="SELECT uid FROM user
WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = ".$me['id'].")
AND is_app_user = 1";
Loop through each user and use feed

Create Auto-POST by Facebook-SDK

I want to enable Facebook Auto Post in my website.
first I created an app, then follow the facebook docs I used the facebook-sdk for PHP, I inserted APP-ID and APP-SECRET, create LOGIN-URL and... to my script (everything like the facebook docs), but I still have problem!!
The problem is:
for first time when user visit my page, he see the login link. when he click on it he will redirect to facebook dialog page for allowing app activities. after this, when facebook redirect user to my canvas page, he see the login link again!! (It seems the getUser() function not worked correctly in my script!).
base on facebook guides the user must see the user profile details... but still the login link is visible.
how can I fix this problem...?
<?php
require_once("libs/facebook.php");
$config = array(
'appId' => 'XXXX',
'secret' => 'XXXX'
);
$fbConnect = new Facebook($config);
$user_id = $fbConnect->getUser();
if($user_id)
{
try {
$userProfile = $fbConnect->api('/me', 'GET');
echo "Name: " . $userProfile['name'];
} catch (FacebookApiException $e) {
$loginUrl = $fbConnect->getLoginUrl();
echo "<a href='" . $loginUrl . "'>LOGIN 2</a>";
}
}
else
{
$loginUrl = $fbConnect->getLoginUrl(array( 'scope' => 'publish_stream' ));
echo "<a href='" . $loginUrl . "'>LOGIN 1</a>";
}
?>
User always see "LOGIN 1"! it means the $user_id is always null (before and after app allowing activities)!! after app allowing (when user for first time click on loginUrl link) I have 'stat' and 'code' in my url query string! but still "LOGIN 1" is visible!
I guess you want to publish a message like "Hey guys, I am now using Aref's superawsome Facebook app now", right?
This can be accomplished with the following lines :
<?php
require_once("/FBAPI/src/facebook.php");
$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', 'redirect_uri'=>'http://www.example.com'));
}
if($user){
try{
$user_profile = $facebook->api('/me');
$access_token = $facebook->getAccessToken();
$vars = array(
'caption' => 'Aref\'s Facebook Application',
'message' => 'Hey guys, I am now using Aref\'s superawsome Facebook App :D',
'name' => 'Test',
'link' => 'http://www.example.com',
'description' => 'Aref\'s Facebook Canvas App',
'picture' => 'http://fbrell.com/f8.jpg'
);
$result = $facebook->api('/me/feed', 'post', $vars);
if($result){
echo "Post was set";
}
else{
echo "Error!";
}
}
catch(FacebookApiException $e){
error_log($e);
$user = NULL;
}
}
else{
echo '<img src="img/login.png"/>';
}
?>

Categories