I want to post on some content on my facebook's friends but my below code is not working.
I have set the permissions of publish_action publish_stream,status_update in my developer APP.
This is the below code:-
require_once 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXX',
));
$user = $facebook->getUser();
$_var = $facebook->getAccessToken();
if ($user)
{
try
{
$user_profile = $facebook->api('/me');
$friends = $facebook->api('/me/friends');
foreach($friends['data'] as $friend) {
$facebook->api('/'.$friend['id'].'/feed', 'post', array(
'message' => 'just a test message',
));
}
}
catch(Exception $e){
echo "errorr";
}
}
Plz provide me some solutions
Related
Im a totally facebook newbie developer.This is the sdk version that i use: click . This is the login part:
<?php
require '../src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXXx',
'cookie' => true
));
?>
<?php
$loginUrl = $facebook->getLoginUrl(array (
'scope' => 'email',
'redirect_uri' => 'http://facebook.ebis-servicii.ro/mytest/test.php'
));
$user = $facebook->getUser();
echo 'Login Here ';
?>
And this is the part where I want to get some informations about the user:
<?php
require '../src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXX',
'cookie' => true
));
$user = $facebook->getUser();
if ($user) {
try{
$user_profile = $facebook->api('/me');
var_dump($user_profile);
$fbid = $user_profile['id']; // To Get Facebook ID
$fbuname = $user_profile['username']; // To Get Facebook Username
$fbfullname = $user_profile['name']; // To Get Facebook full name
$femail = $user_profile['email']; // To Get Facebook email ID
}catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
echo "FBID ".$fbid."<br>";
echo "Email ".$femail."<br>";
echo "Name ".$fbfullname."<br>";
echo "Username ".$fbuname."<br>";
echo "Facebook Picture:<br>";
//echo '<img src="https://graph.facebook.com/'.$_SESSION["USERNAME"].'>;
echo "<img src ='https://graph.facebook.com/".$fbuname."'>";
}
?>
This is the url link where you see the results. So my question is how do I get the username? In the result of the var dump there is no username .
Since v2.0, the username is not included in the result anymore, as you can see in the Facebook docs: https://developers.facebook.com/docs/graph-api/reference/v2.1/user
Else, the App Scoped IDs would be pointless, and they came with v2.0 too. Apps should not be able to get to the "real" profile of the users.
You should be able to use the App Scoped ID to show the user picture:
echo '<img src="https://graph.facebook.com/'.$fbid.'/picture?width=121&height=100" />';
Btw, i would suggest using the new PHP SDK (4.x) for new Apps.
This code always runs the last else part and redirects to the login URL provided by facebook api. If i print the $user variable, it just shows 0 everytime.
$facebook = new Facebook(array(
'appId' => 'app id',
'secret' => 'app secret',
));
$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
$_SESSION['logouturl'] = $logoutUrl;
$_SESSION['user'] = $user_profile['name'];
$_SESSION['id'] = $user_profile['id'];
$_SESSION['type'] = 'facebook';
header('Location: index.php');
} else
{
$loginUrl = $facebook->getLoginUrl();
header('Location: '.$loginUrl);
}
?>
$facebook = new Facebook(array(
'appId' => 'xx',
'secret' => 'xx',
'scope' => 'read_stream, email'
));
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$params = array(
'scope' => 'read_stream, email'
);
$loginUrl = $facebook->getLoginUrl($params);
make sure your app setting on Facebook is good, rest is looking fine and if you have minor problem in you app setting on Facebook developer console it wont work specially domain name and domain url settings
How can i check if the current facebook user like a facebook page?
I have this code,
require_once("/path/to/sdk/facebook.php");
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'APP_ID',
'secret' => 'APP_SECRET',
));
$user = $facebook->getUser();
if ($user) {
try {
$likes = $facebook->api("/me/likes/153649677989200");
if( !empty($likes['data']) )
echo "I like!";
else
echo "not a fan!";
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'user_likes'
));
}
But is not responding if the user like the page, please see the page here
Thanks!
You can use FQL
$isFan = $facebook->api(array(
"method" => "fql.query",
"query" => "SELECT uid FROM page_fan WHERE page_id = '<YOUR PAGE ID>' AND uid = '<UID>"
));
I don't know if you need to do this only using FQL, but there is another easier way:
require_once("/path/to/sdk/facebook.php");
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'APP_ID',
'secret' => 'APP_SECRET',
));
$signed_request = $facebook->getSignedRequest();
$like = $signed_request['page']['liked'];
$like will be empty or 1.
I like to check variables first, like this:
if(is_array($signed_request) && isset($signed_request['page']) {
if($signed_request['page']['liked']) {
// do whatever you want to do if the user likes the page
} else {
// do whatever you want to do if the user don't like the page yet
}
}
I have an app which has been running for a while which has only requested some account information for login purposes. Now I want to use it to publish to the stream. I have added publish_stream to the req_perms but, it still doesn't ask for that permission.
Am I missing something?
<?php
# CREATE FACEBOOK BUTTON
$facebook = new Facebook(array(
'appId' => FACEBOOKAPPID,
'secret' => FACEBOOKSECRET,
'cookie' => false,
));
$fb_session = $facebook->getUser();
$fb_me = null;
// Session based API call.
if ($fb_session) {
try {
$fb_uid = $fb_session;
$fb_me = $facebook->api('/me');
$fb_me['photo'] = 'http://graph.facebook.com/'.$fb_uid.'/picture?type=large';
$_SESSION['login_api'] = 1;
$_SESSION['login_api_details'] = $fb_me;
$_SESSION['login_api_user_id'] = $fb_uid;
# WE ARE GOOD TO GO, LETS GET THE ACCESS TOKEN
$_SESSION['access_token'] = $facebook->getAccessToken();
#header_redirect(SITEURL.'/login');
} catch (FacebookApiException $e) {
error_log($e);
}
}
else{
# LOGIN URL FOR FACE BOOK & request extra stuff
$fb_login_url = $facebook->getLoginUrl(array('req_perms'=>'publish_stream,email,user_about_me,user_birthday,user_website'));
header_redirect($fb_login_url);
}
?>
Try this in your else
$params = array(
'canvas' => 1,
'scope' => 'publish_stream,email,user_about_me,user_birthday,user_website',
'fbconnect' => 1,
'redirect_uri' => 'https://apps.facebook.com/YOURAPP',
);
$fb_login_url = $facebook->getLoginUrl($params);
header_redirect($fb_login_url);
This is my code:
<?php
require 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => '25856*****93875',
'secret' => '7c3e95185d681ee6****8bcef1305352',
'cookie' => true
));
$user = $facebook->getUser();
if($user) {
try {
$statusUpdate = $facebook->api('/me/feed', 'post', array('link' => 'http://www.firsatdergisi.com/'));
} catch (FacebookApiException $e) {
echo $e->getMessage();
}
}
else
{
$url = 'https://www.facebook.com/dialog/oauth?client_id=25856*****93875&redirect_uri=http://apps.facebook.com/gunlukburcpaylas/&scope=email,read_stream,publish_stream';
echo "<a href='". $url ."'>login</a>";
}
?>
Error: (#100) The post's links must direct to the application's connect or canvas URL.
what i should?
The error appears to tell you all you need to know - you can only post a link to your application's connect or canvas url, not directly to an OAuth dialog.