Not able to retrieve any data from graph API - php

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

Related

call to undefined function error in php

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)

facebook api no longer working

<?php
session_start();
//included file and use
$app_id = 'xxx';
$app_secret = 'xxx';
FacebookSession::setDefaultApplication($app_id, $app_secret);
$helper = new FacebookRedirectLoginHelper("`http://example/facebook4.0`/", $app_id, $app_secret);
try
{
$session = $helper->getSessionFromRedirect();
}
catch(FacebookRequestException $ex) { }
catch(Exception $ex) { }
$loggedIn = false;
if (isset($session))
{
if ($session)
{
$loggedIn = true;
try { //logged here and get data
$user_profile = (new FacebookRequest(
$session, 'GET', '/me'
))->execute()->getGraphObject(GraphUser::className());
print_r($user_profile); //print data
}
catch(FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
}
}
if(!$loggedIn) //if user is not online // get link and add scope
{
$loginUrl = $helper->getLoginUrl(array('public_profile','email'));
echo "<a href='$loginUrl'>Login With Facebook</a>";
}
else
{
print_r($user_profile); //logout link is generated here
echo '<br><br>Logout'; //print to sceen
// i dont have more comment to write stack over flow please update it without eating my head to write comments here
}
?>
It was working from last week but not working today at all, after redirecting it display nothing
It can be that your appID and app Secret have changed. Try confirming that .

Get the facebook User name using the API

i'm trying to get the facebook user name using the API
this is my code:
$facebook = new Facebook(array(
'appId' => $this->app_id,
'secret' => $this->app_secret,
'cookie' => true,
));
$session = $facebook->getUser();
$me = null;
if ($session) {
try {
$uid = $facebook->getUser();
echo $uid;
exit();
$me = $facebook->api('/me');
echo $me;
exit();
} catch (FacebookApiException $e) {
error_log($e);
}
$personLastName= $me['last_name'];
var_dump($personLastName);
exit();
the line where it crashes:
$me = $facebook->api('/me');
No errors displayed, just white page and no log error in apache
Some help would be welcome!
Thanks in advance
You are try to print the first element and then you have terminate the statement with exit; then the rest of the code will not be executed.Try this
$me = null;
if ($session) {
try {
$uid = $facebook->getUser();
echo $uid;
$me = $facebook->api('/me');
echo $me;
} catch (FacebookApiException $e) {
error_log($e);
}
$personLastName= $me['last_name'];
var_dump($personLastName);
exit();
}

$facebook->api('/me') not giving the user data

require_once($_SERVER['DOCUMENT_ROOT'] . '/facebook-php-sdk/facebook.php');
$facebook = new Facebook(array(
'appId' => FB_MAIN_APP_API_ID,
'secret' => FB_MAIN_APP_SECRET
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
}catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
Above is the code I'm using to get the Facebook details for my application. In my localhost this is working fine and it gives my Facebook email, user id. After I upload this in to live server this function is not working and it's not giving my Facebook details. I checked on the Facebook app id and it is the correct one.
I cannot figure out why it's not working in live site since it is working in my localhost. Please help me with this. Thank you.
I think problem is in your app which your are using on server. So try the same app which you are using for your localhost and in login and logout url instead of localhost use $_SERVER['HTTP_HOST'].
Example :
$session->fb_logout = $facebook->getLogoutUrl(array('next' => "http://" . $_SERVER['HTTP_HOST'] . "/path/to/logout"));
First check that what $user_profile = $facebook->api('/me'); showing. Just print_r the variable $user_profile.
Ex :
$user_profile = $facebook->api('/me');
print_r($user_profile);
exit;
if it showing blank then surely it will go in catch block. Then write following in catch block.
print_r($e);
exit;
For geting the user id, user need to grant permission
$user_id = $facebook->getUser();
echo $uid;
if($user_id){
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user_id = null;
}
}else{
$login_url = $facebook->getLoginUrl();
echo("<br>login url=".$login_url);
};
After the user grant permission check the login_url, you may access user id.
Try this
Have you checked you have the correct URL in the configuration of your app in Facebook?
I have this piece of code to my FB apps and work properly, could you try it?
$session = $this->facebook->getUser();
if ($session) {
$logoutUrl = $this->facebook->getLogoutUrl();
try {
$data['uid'] = $this->facebook->getUser();
$me = $this->facebook->api($session);
$data['me'] = $me;
} catch (FacebookApiException $e) {
error_log($e);
}
} else {
$loginUrl = $this->facebook->getLoginUrl(
array(
'scope' => 'publish_stream, user_about_me, email',
'redirect_uri' => 'YOUR URL',
'canvas' => 1,
'fbconnect' => 0,
'display' => 'page',
));
}
if (!$session) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
$this->load->view('MY VIEW', $data);

Posting to users wall while applying the app (facebook php sdk)

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.

Categories