Post Image on Facebook Page Feed as Page - php

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/

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

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

Facebook API Giving Server error 500

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.

Facebook PHP SDK Graph returned an error: Invalid OAuth access token

I don't why I am getting this error.
My appid and secret id are all correct. Don't know what went wrong.
<?PHP
$fb = new Facebook\Facebook([
'app_id' => '',
'app_secret' => '',
'default_graph_version' => 'v2.5',
]);
$data = [
'message' => 'My awesome photo upload example.',
'source' => $fb->fileToUpload('C:/xampp/htdocs/laravel5-learning/public/image/share/'.$name),
];
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->post('/me/photos', $data, '{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'];
?>
It is showing this error:
Graph returned an error: Invalid OAuth access token.

Facebook PHP SDK v5 Post to Fan Page

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.

Categories