Graph API method not working - php

Just one day before the following codes working for me but now it's not working for me.
require 'facebook.php';
$facebook = new Facebook(array( 'appId' =>'APPID','secret' => 'some_secret'));
$user = $facebook->getUser();
if ($user){$user_profile = $facebook->api('/me');}
it's not showing the current loggged in user name or id. Is n't it working anymore ???

Looks like you are using some deprecated api/code examples, current facebook docs have this snippet:
$fb = new Facebook\Facebook([
'app_id' => '{app-id}',
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.2',
]);
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/me?fields=id,name', '{access-token}');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$user = $response->getGraphUser();
echo 'Name: ' . $user->getName();

Related

Facebook graph return only the id

I used Facebook SDK for get share count for posts in web site, but when I send the request the responses is only the id
$link = 'url-i-want-get-share-count';
$access_token="My-Access-token";
$app_id="My-app-id";
$app_secret="My-app-secret";
$fb = new \Facebook\Facebook([
'app_id' => $app_id,
'app_secret' => $app_secret,
'default_graph_version' => 'v2.5',
'default_access_token' => $access_token, // optional
]);
try {
$request = $fb->get('/' . $link . '/posts?fields=og_object{engagement}');
} catch(\Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
} catch(\Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
}
var_dump($request);
the response is

Access Token not generated in facebook PHP sdk5?

I am trying from last day to post on user timeline and for this I am using facebook PHP SDK5. I have two files one is fb-integration.php and second is login-callback.php
In fb-intergration.php - I have following code which is basically to check user login or not.
<?php
session_start();
require_once __DIR__ . '/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '2426534xxxxxxxx',
'app_secret' => 'dc36a92xxxxxxxxxxxxxxxxxxxxxxx',
'default_graph_version' => 'v2.8',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'publish_actions']; // optional
$loginUrl = $helper->getLoginUrl('https://example.com/events/test/login-callback.php', $permissions);
echo "<script>window.top.location.href='".$loginUrl."'</script>";
?>
In login-callback file - I have following script:
<?php
session_start();
require_once __DIR__ . '/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '2426534xxxxxxxx',
'app_secret' => 'dc36a92xxxxxxxxxxxxxxxxxxxxxxx',
'default_graph_version' => 'v2.8',
]);
$helper = $fb->getCanvasHelper();
try {
if (isset($_SESSION['facebook_access_token'])) {
$accessToken = $_SESSION['facebook_access_token'];
} else {
$accessToken = $helper->getAccessToken();
}
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if ($accessToken) {
echo 'Successfully logged in!';
}else{
echo 'Sorry not generated';
}
?>
Every time, I am getting result Sorry not generated.
Where am i doing wrong. why is not my accesToken generating?

Facebook login php

I am trying to make a login page for v2.8 graph api.
I am following all the graph api examples but still getting an error.
This is the error:
Facebook SDK returned an error: Cross-site request forgery validation failed. Required param "state" missing from persistent data.
fbaccess.php
session_start();
$app_id = "xxxxxx";
$app_secret = "xxxxx";
$site_url = "http://xxxxxxxxx/";
require_once __DIR__ . '/src/autoload.php';
// Create our application instance
$fb = new Facebook\Facebook([
'app_id' => $app_id,
'app_secret' => $app_secret,
'default_graph_version' => 'v2.8',
]);
index.php
session_start();
include("fbaccess.php");
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'user_likes']; // optional
$loginUrl = $helper->getLoginUrl('http://shortbucks.com/facebook/callback.php', $permissions);
echo 'Log in with Facebook!';
callback.php
session_start();
include("fbaccess.php");
$helper = $fb->getRedirectLoginHelper();
try {
// Returns a `Facebook\FacebookResponse` object
$accessToken = $helper->getAccessToken();
$response = $fb->get('/me?fields=id,name', $accessToken);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$user = $response->getGraphUser();
echo 'Name: ' . $user['name'];
Can someone help me identify the error?
Thanks
Hi I had the same problem, i have downloaded the last version of SDK from Facebook and voila works perfect.. cheers

prblem with post on page of facebook with api

I wrote a code for post a message on my page (not own timeline)
post.php
<?php
require_once __DIR__ . '/vendor/autoload.php';
session_start();
$fb = new Facebook\Facebook([
'app_id' => 'xxxxxxxxxxxxxx',
'app_secret' => 'xxxxxxxxxxxxxx',
'default_graph_version' => 'v2.5',
]);
$data = [
'message' => 'My awesome photo upload example.',
];
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->post('/xxxxxxxxxxxxxx/feed', $data, $_SESSION['facebook_access_token']);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$graphNode = $response->getGraphNode();
echo 'Photo ID: ' . $graphNode['id'];
index.php
<?php
require_once __DIR__ . '/vendor/autoload.php';
session_start();
$app_id = 'xxxxxxxxxxxxxx';
$app_secret = 'xxxxxxxxxxxxxx';
$fb = new Facebook\Facebook([
'app_id' => $app_id,
'app_secret' => $app_secret,
'default_graph_version' => 'v2.4',
'default_access_token' => isset($_SESSION['facebook_access_token']) ? $_SESSION['facebook_access_token'] : 'APP-ID|APP-SECRET'
]);
try {
$response = $fb->get('/xxxxxxxxxxxxxx?fields=id,name');
$user = $response->getGraphUser();
echo 'Name: ' . $user['name'];
//exit; //redirect, or do whatever you want
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
}
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'user_likes', 'manage_pages', 'publish_actions'];
$loginUrl = $helper->getLoginUrl('http://xxxxxxxxxxxxxx/facebook/login-callback.php', $permissions);
echo 'Log in with Facebook!';
login-callback.php
<?php
require_once __DIR__ . '/vendor/autoload.php';
session_start();
$app_id = 'xxxxxxxxxxxxxx';
$app_secret = 'xxxxxxxxxxxxxx';
$fb = new Facebook\Facebook([
'app_id' => $app_id,
'app_secret' => $app_secret,
'default_graph_version' => 'v2.4',
'default_access_token' => 'APP-ID|APP-SECRET'
]);
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
//echo 'Graph returned an error: ' . $e->getMessage();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
//echo 'Facebook SDK returned an error: ' . $e->getMessage();
}
if (isset($accessToken)) {
// Logged in!
$_SESSION['facebook_access_token'] = (string) $accessToken;
} elseif ($helper->getError()) {
// The user denied the request
}
header('Location: index.php');
It's just work when in file post.php use this code:
$response = $fb->post('/me/feed', $data, $_SESSION['facebook_access_token']);
And then post show on my timeline.
But when I want to post on page and use this:
$response = $fb->post('/xxxxxxxxxxxxxx/feed', $data, $_SESSION['facebook_access_token']);
don't work and not appears anythings, and don't show my post on the page or timeline!

Post daily on Facebook with the Facebook SDK

first i want to ask if its possible to post on a facebook page all day a post with the PHP SDK.
Here is my version (it works but the token expires after some hours)
<?php
define('FACEBOOK_SDK_V4_SRC_DIR','../sdks/facebook/');
require_once("../sdks/facebook/autoload.php");
$access_token ="EAACEdEose0cBAGVFnr3vE8U6FYXxsJbRadxZB4ynIWaG5aFzDo0bx6vdIWysh4sPvnOPu3CHZCfLSM9MYLRRlvdeyJazIXgP0h3fqbbNc3oxg24b4I7rZChUS1FNHqm1HsWbym4IjsaoVw4fAqRlYfPsw2iX4nFZB7KDeAu1BglrRg2qGug8";
$app_secret ="c19ca5fc56bc0ddce32c75a3cb53059f";
$fb = new Facebook\Facebook([
'app_id' => '17852532523721',
'app_secret' => $app_secret,
'default_graph_version' => 'v2.4',
]);
$linkData = [
'link' => 'http://www.url.com/',
'message' => "Test"
];
try {
$response = $fb->post("/42935243503912968/feed",$linkData, $access_token);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
?>
It's possibole to get the actual acces_token of my users. Maybe someone now a solution.
Big thanks

Categories