PHP Facebook SDK - php

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';
}

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.';
}?>

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);

Facebook Photo Posting App Doesn't Work?

I used a Facebook Developers script called "Upload photos to a User's profile" (check it out : https://developers.facebook.com/docs/php/howto/uploadphoto) but it doesn't work...
Here's my code :
<?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('facebook/src/facebook.php');
$config = array(
'appId' => 'I_ENTERED_MY_APP_ID_HERE',
'secret' => 'I_ENTERED_MY_APP_SECRET_HERE',
'fileUpload' => true,
'allowSignedRequest' => false // optional but should be set to false for non-canvas apps
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$photo = '/pictures/1.jpg'; // Path to the photo on the local filesystem
$message = 'Look at this picture I just uploaded !';
?>
<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 {
// Upload to a user's profile. The photo will be in the
// first album in the profile. You can also upload to
// a specific album by using /ALBUM_ID as the path
$ret_obj = $facebook->api('/me/photos/', 'POST', array(
'source' => '#' . $photo,
'message' => $message,
)
);
echo '<pre>Photo ID: ' . $ret_obj['id'] . '</pre>';
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' => 'photo_upload'
));
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
// No user, print a link for the user to login
// To upload a photo to a user's wall, we need photo_upload 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' => 'photo_upload') );
echo 'Please login.';
}
?>
</body>
</html>
When I refresh my website page, it doesn't upload the image in my database to my wall... Can anyone tell me what's the problem..?

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

Post a Link from PHP

I have this script:
This code should post a Text and a Link to a WebSite
<?
// 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('facebook.php');
$c = array(
'appId' => '4102137023*****',
'secret' => '*****c4a60cb08*****7c0333*****',
);
$facebook = new Facebook($c);
$uid = $facebook->getUser();
echo "Userid: " . $uid;
echo "<BR>";
?>
<html>
<head></head>
<body>
<?
if($uid){
// 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' => 'angel-craft.de',
'message' => 'Wenn ihr das hier seht freut euch auf ein Game'
));
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.';
echo $e->getType();
echo $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>
This should post a link with a text
but the Userid($uid) stays empty.
And YES this is the Demo script from FB dev.
I've used this successfully.
<?php
require 'src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => '-----',
'secret' => '-----',
'cookie' => true,
));
$session = $facebook->getSession();
$me = null;
// Session based API call.
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
$Name = $me['first_name'];
$properties = array(
array(
'text'=>'Property Link',
'href'=>'http://www.yahoo.com'
),
array(
'text'=>'Property Link',
'href'=>'http://www.yahoo.com'
)
);
// Link that is adjacent to "Like" and "Comment" at the very bottom of the post.
$action_links = array(
'name'=>'Test',
'link'=>'http://www.yahoo.com'
);
// Dictates who can see the post.
$privacy = array(
'value'=>'ALL_FRIENDS'
);
// api('/me/feed', 'post',... = Wall Post.
$wallPost = $facebook->api('me/feed', 'post', array(
'message'=> 'Testing',
'link'=> 'http://www.yahoo.com',
'properties'=>$properties,
'actions'=>$action_links
)
);
} catch (FacebookApiException $e) {
error_log($e);
}
}
$par = array();
$par['req_perms'] = "email, publish_stream";
if ($me) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl($par);
}
?>
I found the bug, The cert was old. Thanks to all :)

Categories