I am trying to get friend-list from facebook using Graph API and PHP I have tired below code its showing error
Error : Undefined function d() in D:\xampp\htdocs\kenshinkan-new\kenshinkan\facebook.php on line 37
How to fix this error ?
<body>
<?php
//facebook application configuration
$fbconfig['appid' ] = "45675467245672462456262";
$fbconfig['secret'] = "afdgadgdagdagadfg";
try{
include_once ('.\facebook-php-sdk-master\src\facebook.php');
}
catch(Exception $o){
print_r($o);
}
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true,
));
$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'email'
)
);
if ($user) {
try {
$user_profile = $facebook->api('/me');
$user_friends = $facebook->api('/me/friends');
$access_token = $facebook->getAccessToken();
} catch (FacebookApiException $e) {
d($e);
$user = null;
}
}
if (!$user) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
$total_friends = count($user_friends['data']);
echo 'Total friends: '.$total_friends.'.<br />';
$start = 0;
while ($start < $total_friends) {
echo $user_friends['data'][$start]['name'];
echo '<br />';
$start++;
}
?>
</body>
</html>
Your error is right here..
catch (FacebookApiException $e) {
d($e); //This is your error. What do you suppose function d() would do?
$user = null;
}
Well, either adding the d() function, or removing the d($e); call to the function (which is not there)?
Also, you'll only be able to retrieve those friends which are also using your app, not all.
It looks like d() - is a short-hand function to dump/log exception.
It's definitely created by that person, who's code you took :)
You can remove this line or define that function. Could be smth like:
function d($e) {
error_log($e->getMessage());
}
error_log() — Send an error message to the defined error handling routines (see http://php.net/manual/en/function.error-log.php)
Related
i am using basic facebook php sdk to login users to my page via facebook.
facebook.php, base_facebook.php and:
$facebook = new Facebook(array(
'appId' => '',
'secret' => '',
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if (!$user) {
header("Location: /");
} else {
$loginUrl = $facebook->getLogoutUrl();
and i want to allow my users to post (or skip) something to their wall's when logging in. like that:
http://developers.facebook.com/attachment/web_dialog.png
any ideas?
to post to a user's wall you'd do something like this:
if (isset($_POST['status'])){
try {
$post = stripslashes($_POST['status']);
$statusUpdate = $facebook->api('/<fb_user_id>/feed?access_token='.$session['access_token'], 'post', array('message'=> strip_tags($post), 'cb' => ''));
} catch (FacebookApiException $e) {
echo "<pre>";
echo $e;
echo "</p>";
}
}
You can style the form that posts to that script to match that in the png file.
hope that is what you are looking for and helps you.
I am using Codeignitor .
I want to post to facebook through my application.
the problem is it always returns you are not logged in.
here what i tried ..
function postTofb()
{
require_once("pocket/fb/facebook.php");
$fbconfig = array();
$fbconfig['appId'] = 'app_id';
$fbconfig['secret'] = 'app_secret';
$fbconfig['baseurl'] = "site_name";
$facebook = new Facebook($fbconfig);
$user = $facebook->getUser();
if($user)
{
try {
$user_profile = $facebook->api('/me');
$ID = $user_profile['id'];
echo $ID;
}
catch (FacebookApiException $e)
{
echo $e;
}
}
else
{
//else starts
$loginUrl = $facebook->getLoginUrl(
array(
'scope'=> 'email,offline_access,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown',
'redirect_uri'=>$fbconfig['baseurl']
)
);
$data["status"] = FALSE;
$data["url"] = $loginUrl;
echo json_encode($data);
//else ends
}
}
now i am calling the function through ajax...but it always response with FALSE...i mean the else part.
here what i am getting
{"status":false,"url":"https:\/\/www.facebook.com\/dialog\/oauth?client_id=281255791959236&redirect_uri=http%3A%2F%2Fwww.MY_SITE_NAME.com%2F&state=3d19a96f48022344222f518609f30537&scope=email%2Coffline_access%2Cpublish_stream%2Cuser_birthday%2Cuser_location%2Cuser_work_history%2Cuser_about_me%2Cuser_hometown"}
looks like i missed some points...please help me..
Thank you.
Offline access is no longer supported by facebook, only thing you can do is generate a token with max 60 days.
The problem was associated with LoginUrl
I removed some of the permission parameters and it worked.
loginUrl = $facebook->getLoginUrl(array("scope")=>"publish_stream");
Thank you.
Hello I am using the below code section. For back ground, last time was working I was getting Curl Exception 77 due to ssl related issues. Still HTTPS is not enabled at my domain yet. With this little info. I am pasting the code section below. There are some prints that I am using for my debugging. (I am new to PHP, kindly bear with me)
<?php
include_once('./php-sdk/facebook.php');
include_once('./php-sdk/fbhelper.php');
$config = array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET,
);
try {
$facebook = new Facebook($config);
}
catch (FacebookApiException $e) {
echo 'Excetption is' . $e->__toString();
}
?>
<?php
$user_id = $facebook->getUser();
if ($user_id) {
echo "user exists and is: " .$user_id;
} else {
echo "user DONT exists";
}
// If i comment out this next try catch block for $session, then my page renders
// fine, but when I am using this code block, then my page doesn't render beyond
// this code section. This is very strange. Why not an exception is thrown if there
// are any issues? Can anybody help me understand this?
try {
$session = $facebook->getSession();
if ($session) {
echo "user exists and is: " . $session;
} else {
echo "user DONT exists";
}
} catch (FacebookApiException $e) {
print_r($e);
}
// now when I comment out above $session related part, then page renders fine, but
// no where any prints show me anything. Why the name is not getting reflected?
if ($user_id) {
try {
$user_profile = $facebook->api('/727850431', 'GET');
echo 'Name: ' . $user_profile['name'];
} catch (FacebookApiException $e) {
print_r($e);
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
}
} else {
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
}
try {
$url = 'https://graph.facebook.com/oauth/access_token?client_id=FACEBOOK_APP_ID&client_secret=FACEBOOK_SECRET& grant_type=client_credentials';
$app_access_token = json_decode(file_get_contents($url));
$graph_url = "https://graph.facebook.com/me?access_token=" . $app_access_token;
$result = json_decode(file_get_contents($graph_url));
print_r($result);
} catch (FacebookApiException $e) {
print_r($e);
}
?>
Trying to post a status update using facebook php sdk. Code posted below. As I understand it, I need ask for extended persmission, which I do in the login URL with '&scope=publish_stream', but this still isn't working.
The resultant login URL looks like:
https://www.facebook.com/login.php?api_key=174954539217971&cancel_url=http%3A%2F%2Fexample.com%2facebook.post.php&display=page&fbconnect=1&next=http%3A%2F%2Fexample.com%2Ffacebook.post.php&return_session=1&session_version=3&v=1.0&scope=publish_stream
So you can see it is being set. While this is essentially the same question as: Facebook OAuthException: "user hasn't authorized the application to perform this action"
The suggested solution in that thread is not working here.
<?php
echo "Posting to facebook..<br/>";
require './fb_src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'appId',
'secret' => 'secret',
'cookie' => true,
));
$session = $facebook->getSession();
$me = null;
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
header('Location: ' . $facebook->getLoginUrl(array('scope' => 'publish_stream')));
error_log($e);
}
}
else {
header('Location: ' . $facebook->getLoginUrl(array('scope' => 'publish_stream')));
}
print_r($me);
try {
$feed = $facebook->api('/me/feed', 'post', array('message' => 'Hello world!', 'cb' => ''));
} catch (FacebookApiException $e) {
print($e);
}
print_r($feed);
?>
parameter is 'req_perms' not 'scope'. So hard to find good documentation...
Works fine now.
I am developing a facebook iframe application with the New Graph API php sdk. I am using the basic code to just display my name. But it does not return any value. Some one please help me with this.
<?php
include_once 'facebook.php';
include_once 'config.php';
$facebook = new Facebook(array('appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET_KEY,
'cookie' => true,));
$session = $facebook->getSession();
$me = null;
if ($session) {
try {
$uid = $facebook->getUser();
echo "Hello " . $uid . "<br />"; // This is displayed with my User Id
$me = $facebook->api('/me');
echo "Hello " . $me['name'] . "<br />"; // This is not displayed.
} catch (FacebookApiException $e) {
error_log($e);
}
}
if ($me) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
Why is my name not displayed?
Edit 1
If I use the example code that comes with the New Graph API facebook sdk zip folder, I get the following error:
Fatal error: Uncaught CurlException: 6:
Could not resolve host: graph.facebook.com;
No data record of requested type thrown in
C:\xampplite\htdocs\newtest\facebook.php on line 513
where line no 513 is:
$e = new FacebookApiException(array(
'error_code' => curl_errno($ch),
'error' => array(
'message' => curl_error($ch),
'type' => 'CurlException',
),
));
Please explain me what is wrong. I searched for the error explanation in google,but i couldn't get much information. What is the error I have made?
try writing the following code in the try block where you have called the this($me = $facebook->api('/me');) api.
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYHOST] = 2;
let us tack a look at the error by the following script:
try {
$uid = $facebook->getUser();
$fbme = $facebook->api('/me');
} catch (FacebookApiException $e) {
print_r($e);
}
Error:
[error] => Array
(
[message] => Failed to connect to 66.220.147.27: Permission denied
[type] => CurlException
)
outgoing connections are filtered by the server firewall. Refer to your hosting support for additional information how to allow the connection to external host.
Note that you can also disable the firewall, and everything will work well.