I've created a new app using version 2.9 of the Facebook PHP API. When trying to share a link, I can't force the post to contain the picture, title, and description fields I've specified.
Instead, Facebook uses Open Graph to dynamically pull an image URL from the shared page as well as its title and description. However, these are not what I want to appear on Facebook.
Here is some sample code:
$linkData = [
'message' => 'A new test post to Facebook',
'link' => 'https://www.example.com',
'picture' => 'https://www.example.com/image.jpg',
'name' => 'Test name',
'caption' => 'Test caption',
'description' => 'Test description'
];
try {
$response = $fb->post('/me/feed', $linkData, $pageAccessToken);
} 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'];
Is there any way to force Facebook to use the custom parameters I've specified?
Related
I want to programatically post a new story on a page I own. Do I need to ask Facebook for permissions to do that? When I invoke Facebook -> post I get an error: (#200) The user hasn't authorized the application to perform this action.
I wanted to request the publish_actions permission, but it seems to be permission for my app to post to app users' walls, which is not what I intend to do, I just want to publish on my own wall.
This is my code:
$fb = new Facebook\Facebook([
'app_id' => 'my app id',
'app_secret' => 'my app secret',
'default_graph_version' => 'v2.9',
'default_access_token' => 'the access token'
]);
$linkData = [
'link' => 'http://example.com/link/for/today',
'message' => 'This is the link for today',
];
try {
$response = $fb->post('/me/feed', $linkData);
} 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();
}
$graphNode = $response->getGraphNode();
echo 'Posted with id: ' . $graphNode['id'];
Thanks for any help!
To post to a page as the page, you will need permissions manage_pages and publish_pages, and you will need to get a page access token first ... and then use that to create the post.
https://developers.facebook.com/docs/facebook-login/access-tokens/#pagetokens
i use facebook api for auto post from my website.
here is my code, after that i explain my problem:
require_once "Facebook/autoload.php";
$fb = new Facebook\Facebook([
'app_id' => 'xxxxxxx',
'app_secret' => 'xxxxxxxxx',
'default_graph_version' => 'v2.2'
]);
$pageAccessToken = 'xxxxxxxxxxxxxxxxxxxxx';
$linkData = [
'link' => $link,
'message' => $message,
'picture' => $pic
];
try {
$response = $fb->post('/me/feed', $linkData, $pageAccessToken);
} 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();
}
$graphNode = $response->getGraphNode();
my problem is that, this code work sometimes, not all time.
and in sometime i see these errors:
Severity: Notice
Message: Undefined offset: 1
Filename: Http/GraphRawResponse.php
Line Number: 108
and
Severity: 4096
Message: Argument 4 passed to Facebook\FacebookResponse::__construct() must be of the type array, null given, called in /home/deponews/public_html/application/controllers/Facebook/FacebookClient.php on line 225 and defined
Filename: Facebook/FacebookResponse.php
Line Number: 75
i go to api explorer , and select my page from drop down and at the bottom drop down , select access page token , then select my page name again.
and copy generation token into my page access token.
i don't know why sometimes this code work well , and sometimes its not.
any idea ?
tnx
I'm using the tutorial here http://www.adamboother.com/blog/automatically-posting-to-a-facebook-page-using-the-facebook-sdk-v5-for-php-facebook-api/ to post to a facebook page using the PHP sdk.
I have create the app on Facebook, and I have created the Never Expire Page Access Token, which I have verified using the Access Token Debug Tool https://developers.facebook.com/tools/debug/accesstoken/
However, when I run my test code, it does NOT return any errors, but I get no posts on my page. I get a response back from my print_r($graphNode) of the following
Facebook\GraphNodes\GraphNode Object
(
[items:protected] => Array
(
[id] => 1204777049583245_1264619360265680
)
)
The code for my test post is below, can anyone explain to me WHY I am not getting any posts to show up?
define("FACEBOOK_APP_ID","MY APP ID");
define("FACEBOOK_APP_SECRET","MY APP SECRET");
define("FACEBOOK_APP_TOKEN","MY PAGE ACCESS TOKEN");
define("FACEBOOK_SDK_V4_SRC_DIR",$_SERVER['DOCUMENT_ROOT'].'/socialmedia/Facebook/src/Facebook/');
require_once($_SERVER['DOCUMENT_ROOT'].'/socialmedia/Facebook/src/Facebook/autoload.php');
$fb = new Facebook\Facebook([
'app_id' => FACEBOOK_APP_ID,
'app_secret' => FACEBOOK_APP_SECRET,
'default_graph_version' => 'v2.2',
]);
//Post property to Facebook
$linkData = [
'link' => 'www.yoururl.com',
'message' => 'test message'
];
$pageAccessToken = FACEBOOK_APP_TOKEN;
try {
$response = $fb->post('/me/feed', $linkData, $pageAccessToken);
} 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 '<pre>';
print_r($graphNode);
echo '</pre>';
exit;
I'm trying to post a picture with publish_stream permissions from my site onto Facebook. For some reason, the picture doesn't show up when I post but everything else works fine.
By accessing http://www.mysite.com/image/questions.png, the picture shows up correctly. Am I missing something?
$body = array(
'name' => 'Join this site',
'message' => '',
'description' => 'Check this out',
'picture' => 'http://www.mysite.com/image/questions.png',
'link' => SITE_URL,
);
$batchPost = array();
Then I batch post.
Double check to see if it really works.
$body = array(
'name' => 'Join this site',
'message' => '',
'description' => 'Check this out',
'picture' => 'http://www.mysite.com/image/questions.png',
'link' => SITE_URL,
);
$batchPost = array();
$status = $facebook->api('/me/feed', 'POST', $body);
if (isset($status['id'])) //Add a check
{
echo "Message posted to wall!";
}
If not you can use JavaScript SDK FB.ui method to post to Facebook Wall. It is more flexible in my opinion as users can input their own message, unlike how it is defined by the developer through the PHP SDK method.
JavaScript SDK FB.ui Method
<script>
function postToWall()
{
var obj = {
method: "feed",
name: "Join this site",
description: "Check this out",
caption: "CAPTION",
picture: "http://www.mysite.com/image/questions.png",
link: "SITE_URL"
};
function callback(response) {
//do something
}
FB.ui(obj, callback);
}
</script>
More on JavaScript SDK Feed Dialog: http://developers.facebook.com/docs/reference/dialogs/feed/
Edit: Use another image and try again. http://www.mysite.com/image/questions.png has been removed. Upload your picture on some image hosting website instead of hotlinking the image directly.
Using the Graph API Explorer, I tried posting the following: /[another_user_id]/links and a link field
but I am getting this error:
(#240) Requires a valid user is specified (either via the session or via the API parameter for specifying the user.
What am I missing here? Using /[another_user_id]/feed works but I want it to be a shared link.
The Following Code Posts In Users Friends wall.
try {
$newStatus = $facebook->api("/$FRIENDSUSERID/feed", 'POST',
array(
'link' => "https://apps.facebook.com/XXXXXXXXX",
'picture' => "https://XXXXXXXXX", //colour
'name' => "XXXXXXXXX",
'description' => "XXXXXXXXX",
'access_token' => $access_token
));
//echo '<pre>Post ID: ' . $newStatus['id'] . '</pre>';
} catch(FacebookApiException $e) {
error_log($e->getType());
error_log($e->getMessage());
//echo '<textarea style="width: 300px; height: 200px;">' . $e->getMessage() . '</textarea>';
}