I'm having a lot of trouble getting the Facebook API to work at all. Any example code I use gives me Server Error 500. I'm trying to post a link to my facebook page (this code is from the example code Facebook gives in its documentation). To clarify as well, I do have an app-id, secret-id, and I believe I got the right access token from the Graph API. I know I need a public_action permission to post a link, but the way I read it, I think the access token defaults with public_action.
<?php
$appid = '{app-id}';
$appsecret = '{app-secret}';
$redirect_uri = 'http://mywebsite.com';
// Define the root directoy
define( 'ROOT', dirname( __FILE__ ) . '/' );
// Autoload the required files
require_once( ROOT . 'facebook-php-sdk-v4-4.0-dev/autoload.php' );
$fb = new Facebook\Facebook([
'app_id' => '{app-id}',
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.2',
]);
$linkData = [
'link' => 'http://www.example.com',
'message' => 'User provided message',
];
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->post('/me/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;
}
$graphNode = $response->getGraphNode();
echo 'Posted with id: ' . $graphNode['id'];
?>
I really do not know what I am doing wrong. Likewise, where it says "me" should that be my facebook user id? Thank you.
Related
Using this example from fb-sdk:
require_once 'fb-sdk/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '123456',
'app_secret' => 'abc1233',
'default_graph_version' => 'v2.6',
]);
$data = [
'message' => 'Testupload',
'source' => $fb->fileToUpload('image.jpg'),
];
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->post('/PageID/photos', $data, 'XXXXXXYYYYYY');
} 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'];
i am able to post as myself to the wall of my facebook-page.
'app_id' and 'app_secret' come from the app i created for this.
If i set $response = $fb->post('/me/photos', $data, 'XXXXXXYYYYYY'); the post shows up on my timeline.
What do i need to change, so get a photo, posted by "Page" on the feed of "Page"?
You need to use a Page Token with the publish_pages permission to post "as Page". You can get a Page Token with the following API call, after authorizing with manage_pages and publish_pages:
/page-id?fields=access_token
More information:
https://developers.facebook.com/docs/facebook-login/access-tokens
http://www.devils-heaven.com/facebook-access-tokens/
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 ?
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();
?>
I try to post simple message on my facebook post wall, I try to use PHP SDK v5. The documentation about sdk witch is on Facebook Developers is not so clear for me, and I don't understand what I'm doing wrong.
When i try run this script with posting a message a get an error message " Graph returned an error: Invalid parameter"
This is my code:
<?php
session_start();
require_once __DIR__ . '/vendor/facebook-php-sdk-v4-5.0-dev/src/Facebook/autoload.php';
//use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\Authentication\AccessToken;
//use Facebook\GraphUser;
//use Facebook\FacebookRequestException;
$fb = new Facebook\Facebook([
'app_id' => '************',
'app_secret' => '****************************',
'default_graph_version' => 'v2.4',
]);
$page_id = '****************';
// HERE IS LONG LIVED ACCESS TOKEN WITCH I GENERATED MANUALY
$access_token = '*********************************';
// HERE IS A CODE WITH GET A POSTS FROM MY WALL AND PRINT IT IN A MY WEBSITE
$object = $fb->get('.$page_id.'/feed', $access_token);
$posts_array = $object->getDecodedBody();
print_r($posts_array['data']);
$linkData = [
'link' => 'http://www.example.com',
'message' => 'User provided message',
];
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->post('/'.$page_id.'/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;
}
$graphNode = $response->getGraphNode();
echo 'Posted with id: ' . $graphNode['id'];
Thank you for your help !
I found the problem. In the $linkData array the link parameter is a problem. When i deleted it, every think works. Topic to close.
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');
?>