Trouble in facebook PHP SDK, can't make batch requests - php

I am trying to build an application that can retrieve these data from a group that I manage:
-> member's name
-> the admin who added each member
I've 3 separate files :
index.php: contains the logging using group permissions:
<?php
session_start();
require_once __DIR__ . '/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => censored,
'app_secret' => censored,
'default_graph_version' => 'v2.4', // or use v2.5 latest version
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['user_managed_groups'];
$redirectUrl = 'http://localhost/test/fb/vendor/fbapp.php';
$loginUrl = $helper->getLoginUrl($redirectUrl, $permissions);
echo 'Log in with Facebook!';
?>
fbapp.php: retrieve the Facebook Access Token.
<?php
session_start();
require_once __DIR__ . '/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => censored,
'app_secret' => "censored",
'default_graph_version' => 'v2.4', // or use v2.5 latest version
]);
helper = $fb->getRedirectLoginHelper();
try {
$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 (isset($accessToken)) {
// Logged in!
$_SESSION['facebook_access_token'] = (string) $accessToken;
echo "Logged in \n";
$redirectUrl = 'http://localhost/test/fb/vendor/traitement.php';
echo 'nextStep';
// Now you can redirect to another page and use the
// access token from $_SESSION['facebook_access_token']
}
?>
traitement.php: perform graph requests (here where problems appears)
<?php
session_start();
require_once __DIR__ . '/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => censored,
'app_secret' => "censored",
'default_graph_version' => 'v2.4', // or use v2.5 latest version
]);
$accessToken = $_SESSION['facebook_access_token'];
$fb->setDefaultAccessToken($accessToken);
// Get user groups detail
$requestUserManagedGroups = $fb->request('GET', '/me/groups?fields=members,from');
//Make a batch request, this is not working, why ?
$batch = ['user-groups' => $requestUserLikes];// anyother request is not working (tried to change $requestUserLikes by other values..)
try {
$responses = $fb->sendBatchRequest($batch);
} 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;
foreach ($responses as $key => $response) {
if ($response->isError()) {
$e = $response->getThrownException();
echo '<p>Error! Facebook SDK Said: ' . $e->getMessage() . "\n\n";
echo '<p>Graph Said: ' . "\n\n";
var_dump($e->getResponse());
} else {
echo "<p>(" . $key . ") HTTP status code: " . $response->getHttpStatusCode() . "<br />\n";
echo "Response: " . $response->getBody() . "</p>\n\n";
echo "<hr />\n\n";
}
}
?>
I come up with this error msg:
Notice: Undefined variable: requestUserLikes in/home/yassine/srv/test/fb/vendor/traitement.php on line 20
Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Argument for add() must be of type array or FacebookRequest.' in /home/yassine/srv/test/fb/vendor/facebook/php-sdk-v4/src/Facebook/FacebookBatchRequest.php:85 Stack trace: #0 /home/yassine/srv/test/fb/vendor/facebook/php-sdk-v4/src/Facebook/FacebookBatchRequest.php(78): Facebook\FacebookBatchRequest->add(NULL, 'user-groups') #1 /home/yassine/srv/test/fb/vendor/facebook/php-sdk-v4/src/Facebook/FacebookBatchRequest.php(61): Facebook\FacebookBatchRequest->add(Array) #2 /home/yassine/srv/test/fb/vendor/facebook/php-sdk-v4/src/Facebook/Facebook.php(492): Facebook\FacebookBatchRequest->__construct(Object(Facebook\FacebookApp), Array, Object(Facebook\Authentication\AccessToken), 'v2.4') #3 /home/yassine/srv/test/fb/vendor/traitement.php(23): Facebook\Facebook->sendBatchRequest(Array) #4 {main} thrown in /home/yassine/srv/test/fb/vendor/facebook/php-sdk-v4/src/Facebook/FacebookBatchRequest.php on line 85
EDIT: I fixed this error by simply noticing that i needed to change $requestUserLikes by $requestUserManagedGroups, !!
I come up with a new problem is that I'm not being able to see the groups that I manage using this request: "me?fields=groups"
I'm not able to see some good results even in https://developers.facebook.com/tools/explorer/ can anyone please try if they can see their groups using the facebook explorer ?

Related

Failed to connect to graph.facebook.com port 443: Connection refused

I am new to FB graph API and I try to include the FB login in my website, I following the official FB login tutorial from their website, But after login I get the following error:
Failed to connect to graph.facebook.com port 443: Connection refused
Login.php
<?php
session_start();
require_once __DIR__ . '/fbsdk/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'xxx',
'app_secret' => 'xxx',
'default_graph_version' => 'v2.3',
// . . .
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['public_profile', 'email']; // optional
$loginUrl = $helper->getLoginUrl('http://-myserver-/login-callback.php', $permissions);
echo 'Log in with Facebook!';
?>
login-callback.php
<?php
session_start();
require_once __DIR__ . '/fbsdk/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'xxx',
'app_secret' => 'xxx',
'default_graph_version' => 'v2.3',
// . . .
]);
$helper = $fb->getRedirectLoginHelper();
try {
$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 (isset($accessToken)) {
// Logged in!
$_SESSION['facebook_access_token'] = (string) $accessToken;
// Now you can redirect to another page and use the
// access token from $_SESSION['facebook_access_token']
}
?>
example-canvas-app.php
<?php
session_start();
require_once __DIR__ . '/fbsdk/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'xxx',
'app_secret' => 'xxx',
'default_graph_version' => 'v2.3',
// . . .
]);
$helper = $fb->getCanvasHelper();
try {
$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 (isset($accessToken)) {
// Logged in.
}
?>
I already searched in stack and google but I can't find any solution
Hostinger's free server does not allow a connection to Fb's SDK as mentioned by their customer service.

How to post to facebook page directly from php sdk without login

I don't know how to configure for facebook sdk. I just do with google search instruction. But I always get this error finally of every error fix. I use latest facebook php sdk from facebook official page..I find this error on google..
this is facebook link post example.. i just do testing multiple ways..Or May you explain how to configure out facebook php sdk..Thank
<?php
require_once __DIR__ . '/src/Facebook/autoload.php';
require_once __DIR__ . '/src/Facebook/Facebook.php';
use Facebook\Facebook;
use Facebook\autoload;
use Facebook\Exception;
$fb = new Facebook([
'app_id' => 'my-app-id',
'app_secret' => 'my-app-secret',
'default_graph_version' => 'v2.5'
]);
$linkData = [
'link' => 'http://www.example.com',
'message' => 'User provided message',
];
/* $helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} */
//var_dump($fb);
// $fb->setExtendedAccessToken();
// Get access short live access token
$accessToken ='REMOVED';
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->post('/me/feed', $linkData, $accessToken);
var_dump($response);die;
} catch(FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$graphNode = $response->getGraphNode();
echo 'Posted with id: ' . $graphNode['id'];
Fatal error: Uncaught exception
'Facebook\Exceptions\FacebookAuthenticationException' with message
'Invalid appsecret_proof provided in the API argument' in
C:\wamp\www\facebook-php-sdk-v4-5.0.0\src\Facebook\Exceptions\FacebookResponseException.php:100
Stack trace: #0
C:\wamp\www\facebook-php-sdk-v4-5.0.0\src\Facebook\FacebookResponse.php(210):
Facebook\Exceptions\FacebookResponseException::create(Object(Facebook\FacebookResponse))
1 C:\wamp\www\facebook-php-sdk-v4-5.0.0\src\Facebook\FacebookResponse.php(255):
Facebook\FacebookResponse->makeException() #2
C:\wamp\www\facebook-php-sdk-v4-5.0.0\src\Facebook\FacebookResponse.php(82):
Facebook\FacebookResponse->decodeBody() #3
C:\wamp\www\facebook-php-sdk-v4-5.0.0\src\Facebook\FacebookClient.php(225):
Facebook\FacebookResponse->__construct(Object(Facebook\FacebookRequest),
'{"error":{"mess...', 400, Array) #4
C:\wamp\www\facebook-php-sdk-v4-5.0.0\src\Facebook\Facebook.php(504):
Facebook\FacebookClient->sendRequest(Object(Facebook\FacebookRequest))
5 C:\wamp\www\facebook-php in C:\wamp\www\facebook-php-sdk-v4-5.0.0\src\Facebook\Exceptions\FacebookResponseException.php
on line 100

posting as a page not as an individual with facebook php sdk 5.0.0

I've searched all over for this, the 'answers' I got were outdated. Anytime I run the script it posts the message with my profile to the facebook page even though I have admin permissions.
This is the code I'm using
<?
require_once __DIR__ . '/Facebook/autoload.php';
session_start();
$fb = new Facebook\Facebook([
'app_id' => '',
'app_secret' => '',
'default_graph_version' => 'v2.4'
]);
$page_id="";
$accessToken='Access token I got from https://developers.facebook.com/tools/explorer/145634995501895/';
// Sets the default fallback access token so we don't have to pass it to each request
$fb->setDefaultAccessToken('');
try {
$response = $fb->get('/me');
$userNode = $response->getGraphPage();
} 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;
}
echo 'Logged in as ' . $userNode->getName();
?>

Facebook SDK returned an error: Connection timed out after 10001 milliseconds

i have integrated facebook login (PHP Sdk Version 5.0.0) with my website. That is working fine in my localhost. But it shows the following error when i have hosted in my server;
Facebook SDK returned an error: Connection timed out after 10001
milliseconds
I have just changed the 'Site URL' and 'OAuth redirect URI' only when i hosted in server. Is it necessary to change anything else in my app? I have checked curl is enabled in my server, its showing 'enabled' in php ini. My PHP Version is 5.5.28.
My code is given below;
index.php:
<?php
session_start();
error_reporting(E_ALL);
$path = realpath(dirname(__FILE__));
require $path .'/facebook-sdk-v5/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'xxxxxxx',
'app_secret' => 'xxxxxxxx',
'default_graph_version' => 'v2.4',
]);
$helper = $fb->getCanvasHelper();
// Grab the signed request entity
$sr = $helper->getSignedRequest();
// Get the user ID if signed request exists
$user = $sr ? $sr->getUserId() : null;
if ($user)
{
try
{
// Get the access token
$accessToken = $helper->getAccessToken();
}
catch (Facebook\Exceptions\FacebookSDKException $e)
{
// There was an error communicating with Graph
echo $e->getMessage();
exit;
}
}
else
{
$helper = $fb->getRedirectLoginHelper();
$permissions = ['public_profile','email','user_birthday','user_location']; // optional
$callback = 'http://www.example.com/ret_page.php';
$loginUrl = $helper->getLoginUrl($callback, $permissions);
echo 'Log in with Facebook!';
}
ret_page.php:
<?php
session_start();
error_reporting(E_ALL);
$path = realpath(dirname(__FILE__));
require $path .'/facebook-sdk-v5/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'xxxxxx',
'app_secret' => 'xxxxxxxxxxx',
'default_graph_version' => 'v2.4',
]);
$helper = $fb->getRedirectLoginHelper();
try
{
$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 (!isset($accessToken))
{
if ($helper->getError())
{
header('HTTP/1.0 401 Unauthorized');
echo "Error: " . $helper->getError() . "\n";
echo "Error Code: " . $helper->getErrorCode() . "\n";
echo "Error Reason: " . $helper->getErrorReason() . "\n";
echo "Error Description: " . $helper->getErrorDescription() . "\n";
}
else
{
header('HTTP/1.0 400 Bad Request');
echo 'Bad request';
}
exit;
}
// Logged in
echo '<h3>Access Token</h3>';
var_dump($accessToken->getValue());
$fb->setDefaultAccessToken($accessToken);
$res = $fb->get('/me?fields=id,name,gender,email,birthday,location');
$user = $res->getGraphObject();
// Printing result
print_r($user);

Error when making a GET request to Facebook using Facebook's PHP SDK

I've been trying to work out how to send a Facebook notification using their latest PHP SDK for almost a week now, but I can't seem to get it to work. I've tried everything I could find online but I keep running into errors.
So I decided to try a simple GET request using PHP instead.
To do this I'm trying to use the sample code Facebook gives here:
https://developers.facebook.com/docs/php/FacebookRequest/5.0.0
This is the code I'm trying:
(I've replaced my App ID and App Secret here)
<?php
require_once __DIR__ . '/facebook-sdk-v5/autoload.php';
$fbApp = new Facebook\FacebookApp('123456789', 'a1b2c3d4e5f6g7h8i9');
$request = new Facebook\FacebookRequest($fbApp, '123456789|a1b2c3d4e5f6g7h8i9', 'GET', '/me');
// OR
//$fb = new Facebook\Facebook(/* . . . */);
//$request = $fb->request('GET', '/me');
// Send the request to Graph
try {
$response = $fbApp->getClient()->sendRequest($request);
} 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;
}
$graphNode = $response->getGraphNode();
echo 'User name: ' . $graphNode['name'];
?>
This isn't working, it's giving me the error:
Fatal error: Call to undefined method Facebook\FacebookApp::getClient() in /home/mywebsite/public_html/Games/Barre/phptest.php on line 15
I don't understand what's wrong, I think I'm following Facebook's documents correctly, but it's not working. It seems to be saying that getClient() isn't included in the facebook-sdk-v5 files I've included at the start. I've made sure that I'm pointing to the correct directory for the files and they're all present and correct in that folder (facebook-sdk-v5). Can anyone here can see where I'm going wrong?
I'd really appreciate any advice on how to solve this. Thank you in advance!
UPDATE
<?php
require_once __DIR__ . '/facebook-sdk-v5/autoload.php';
// $fbApp = new Facebook\FacebookApp('123456789', 'a1b2c3d4e5f6g7h8i9');
// $request = new Facebook\FacebookRequest($fbApp, '123456789|a1b2c3d4e5f6g7h8i9', 'GET', '/me');
// OR
$fb = new Facebook\Facebook([
'app_id' => '{123456789}',
'app_secret' => '{a1b2c3d4e5f6g7h8i9}',
'default_graph_version' => 'v2.3',
'default_access_token' => '{123456789|a1b2c3d4e5f6g7h8i9}',
]);
$request = $fb->request('GET', '/me');
// Send the request to Graph
try {
$response = $fb->getClient()->sendRequest($request);
} 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;
}
$graphNode = $response->getGraphNode();
echo 'User name: ' . $graphNode['name'];
?>
Error in console:
Graph returned an error: Invalid OAuth access token signature
2nd UPDATE
This code is working correctly:
<?php
require_once __DIR__ . '/facebook-sdk-v5/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '{123456789}',
'app_secret' => '{a1b2c3d4e5f6g7h8i9j}',
'default_graph_version' => 'v2.3',
]);
$request = $fb->post('/USERIDGOESHERE/notifications', array( 'template' => 'This is a test notification!'), '123456789|a1b2c3d4e5f6g7h8i9j');
?>

Categories