I'm using php facebook api 4.0. I'm using below code to post photo on my timeline.
FacebookSession::setDefaultApplication($app_id, $app_sc);
$helper = new FacebookRedirectLoginHelper($url_page);
$loginUrl = $helper->getLoginUrl();
$session = new FacebookSession($access_token);
FacebookSession::enableAppSecretProof(false);
if($session) {
try {
$response = (new FacebookRequest(
$session, 'POST', '/me/photos', array(
'source' => '#' . realpath('1.jpg'),
'message' => 'message'
)
))->execute()->getGraphObject();
// If you're not using PHP 5.5 or later, change the file reference to:
// 'source' => '#/path/to/file.name'
echo "Posted with id: " . $response->getProperty('id');
} catch(FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
}
But I'm getting Exception occured, code: 324 with message: (#324) Requires upload file error. I think one thing is that, fileupload support should be set to on. But I don't know from where should I turn it on.
Related
I have a problem with FB PHP SDK.
Here is my code:
require_once("autoload.php"); // set the right path
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphObject;
use Facebook\FacebookRequestException;
$APP_ID = 'APP_ID';
$APP_SECRET = 'APPSECRET';
$TOKEN = "TOKEN";
$ID = "PAGE_ID"; // your id or facebook page id
FacebookSession::setDefaultApplication($APP_ID, $APP_SECRET);
$session = new FacebookSession($TOKEN);
$params = array(
"message" => "Xoşbəxt olmamaq haqsızlıq deyilmi özümüzə qarşı?!"
);
if ($session) {
try {
$response = (new FacebookRequest(
$session, 'POST', '/' . $ID . '/feed', $params
))->execute()->getGraphObject();
echo "Posted with id: " . $response->getProperty('id');
} catch (FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
}
But when i try to run this code it returns an error:
Exception occured, code: 200 with message: (#200) Permissions error
in some forums i read that may be i need to approve premissons/ but when i submited premissions to FB they answered that if i am using it only for my purpose (as app admin) premisson approval is not requred.
What can be problem?
Debug your Access Token: https://developers.facebook.com/tools/debug/
Make sure it includes the manage_pages and publish_pages permission. And make sure you are using a Page Token. More information about the different Tokens:
https://developers.facebook.com/docs/facebook-login/access-tokens
http://www.devils-heaven.com/facebook-access-tokens/
I would like to use the Facebook PHP SDK v5 to automatically post to a FAN PAGE. I have the code below which successfully posts to my own wall, but how do I alter the code to post to my fan page? From what I've read, I need to pass in the page id of the fan page?
$params["message"] = 'test';
$params["link"] = 'https://example.com';
$params["picture"] = 'https://example.com/images/logo_hod.jpg';
$params["description"] = 'testtt';
$access_token = $accessToken;
try {
$response = $fb->post('/me/feed', $params, $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 tried replacing this line:
$response = $fb->post('/me/feed', $params, $access_token);
with this to substitute in the fan page id:
$response = $fb->post('/229107363768165/feed', $params, $access_token);
and got the error:
Graph returned an error: Unsupported post request.
UPDATE: I also made the app "public" in an attempt to get past the "unsupported post request" error. No luck.
i think your $params is not right.
I use this:
$fb = new Facebook\Facebook([
'app_id' => '{app-id}',
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.2',
]);
$params = [
'link' => 'https://example.com',
'photo' => 'https://example.com/images/logo_hod.jpg',
'message' => 'A test post!',
];
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->post('/me/feed', $params, '{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;
}
Post Links Using the Graph API
I assumes that you've already obtained an access token and the access token must have the publish_actions permission for this to work.
What I'm doing is creating a post with the Facebook PHP SDK(v4)(and the app is set to use the v2.3 API) with the privacy settings of value to SELF, then later on changing the value to CUSTOM, and allow to the ids of the friends that have accepted the app.
Until recently everything worked fine, and now out of the blue I'm getting this error(nothing changed on our part): "(#100) Object does not support message editing", which I couldn't locate in their documentation. Has anybody encountered this situation and this error?
Here is the code I'm using (like I said everything worked fine until recently)(if you need more details please let me know)
$fb_token = 'FB_TOKEN';
$FB_session = new FacebookSession($fb_token);
try {
$FB_session->validate();
} catch (FacebookRequestException $ex) {
// Session not valid, Graph API returned an exception with the reason.
//echo $ex->getMessage();
throw new RestException(501,'FB error: '. $ex->getMessage());
} catch (\Exception $ex) {
// Graph API returned info, but it may mismatch the current app or have expired.
//echo $ex->getMessage();
throw new RestException(501,'FB error: '. $ex->getMessage());
}
//make posting request to FB
if($FB_session) {
try {
$fb_array = array(
'privacy' => array(
'value' => 'CUSTOM',
'allow' => 'USER_ID'
)
);
$fb_req = new FacebookRequest(
$FB_session, 'POST', "/POST_ID", $fb_array
);
$response = $fb_req->execute()->getGraphObject();
//echo "Posted with id: " . $response->getProperty('id');
$fb_post_id = $response->getProperty('id');
} catch(FacebookRequestException $e) {
//echo "Exception occured, code: " . $e->getCode();
//echo " with message: " . $e->getMessage();
throw new RestException(501,'FB error: '. $e->getCode() .'-'. $e->getMessage());
}
}
I used to create several apps before, but now with the v4 I can't make a simple photo upload to work :( I tried with the 'url' and with the 'source' parameter as well, but nothing works. I want to upload a photo to my own Facebook Page with some caption. I got the access token, the Access Token Debugger says, that it never expires and it has public_profile, read_stream, read_insights, manage_pages, publish_actions, user_photos permissions.
This my code right now:
$accessToken = "MY-ACCESS-TOKEN";
$session = new FacebookSession($accessToken);
$myfile = "file.png"; //created and saved via PHP GD functions
if ( isset( $session ) ) {
try {
$message = "This is my message";
$request = new FacebookRequest(
$session,
'POST',
'/'.$pageid.'/photos',
array (
'source' => realpath("/path/to/" . $myfile),
'message' => $message,
)
);
} catch (FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
} catch (\Exception $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
$response = $request->execute();
$graphObject = $response->getGraphObject();
/* handle the result */
} else {
// show login url
echo 'Login';
}
Right now I get this error:
mod_fcgid: stderr: PHP Fatal error: Uncaught exception 'Facebook\\FacebookAuthorizationException' with message '(#324) Requires upload file' in /data/domains/
What am I doing wrong? Can someone help me, please?
Thank you very much!
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');
?>