Facebook Posting API Question - facebook

on my site right now I'm trying to have it post to a users wall a media file.
I had it working on the old api before, but now I'm trying to get it working on the new one and I'm having an issue.
I'm running this
$facebook->api_client->stream_publish($message, $attachment, $action_links);
Is that the old api or the new one? Because I'm getting this error
Call to a member function stream_publish() on a non-object in
I was reading a tutorial and it said to do this
$statusUpdate = $facebook->api('/me/feed', 'post', array('message'=> 'the message', 'cb' => ''));
but how can I post an attachment/ action links using that?
Thanks!

First of all, don't confuse APIs with SDKs. The newest PHP SDK is capable of communicating with both the new Graph API and the old REST API.
And you are using the new SDK, since Facebook::$api_client doesn't exist in the new SDK.
Secondly, as is usual with most tutorials, they only show you a snapshot of the full features of a system. See here for more details about publishing with the Graph API.
However, you can still use the old API to publish your message like so
$facebook->api( array(
'method' => 'stream.publish'
, 'target_id' => $facebook->getUser()
, 'message' => $message
, 'attachment' => $attachment
, 'action_links' => $action_links
) );

Related

Facebook "Boost Post" through API?

I've been crawling through the documentation and found out that it IS possible to achieve a "Boost Post" functionality through the Facebook Ad APIs. However, I have had some trouble finding what exactly the Boost Post does? i.e. Which part of the API corresponds the "Boost Post" functionality of the Facebook UI?
https://developers.facebook.com/docs/marketing-api/adcreative/v2.4
This page outlines several types of ads. What are the types Facebook "Boost Post" button makes? Or is this wrong part of the API?
See the example for creating an ad_campaign here: https://developers.facebook.com/docs/marketing-api/reference/ad-campaign#Creating
The object (page post in this case) you're trying to promote is set as the promoted object.
You can also set the lifetime or daily budget of the ad at the campaign level.
From the Facebook docs,
For creating an ad from Page post ( boosting a post ), you will first need to create the creative for that ad from the post.
See the doc page on how to create ad adcreatives. Search for Create an ad from an existing page post
use FacebookAds\Object\AdCreative;
use FacebookAds\Object\Fields\AdCreativeFields;
$creative = new AdCreative(null, 'act_<AD_ACCOUNT_ID>');
$creative->setData(array(
AdCreativeFields::NAME => 'Sample Promoted Post',
AdCreativeFields::OBJECT_STORY_ID => <POST_ID>,
));
$creative->create();
After that you will need to create an ad using that creative ad.
Creating ads from API with creative id
require __DIR__ . '/vendor/autoload.php';
use FacebookAds\Object\AdAccount;
use FacebookAds\Object\Ad;
use FacebookAds\Api;
use FacebookAds\Logger\CurlLogger;
$access_token = '<ACCESS_TOKEN>';
$app_secret = '<APP_SECRET>';
$app_id = '<APP_ID>';
$id = '<AD_ACCOUNT_ID>';
$api = Api::init($app_id, $app_secret, $access_token);
$api->setLogger(new CurlLogger());
$fields = array(
);
$params = array(
'name' => 'My Ad',
'adset_id' => '<adSetID>',
'creative' => array('creative_id' => '<adCreativeID>'),
'status' => 'PAUSED',
);
echo json_encode((new AdAccount($id))->createAd(
$fields,
$params
)->exportAllData(), JSON_PRETTY_PRINT);
The examples on the above are using Facebook PHP Business SDK, but you can make the calls using the Facebook PHP Graph SDK with the same parameters.
See the respective SDK files for finding the exact API parameters name.
For example : the Business SDK parameter
AdCreativeFields::OBJECT_STORY_ID is object_story_id as the API parameter.
Hope that helps
I think what you want is a "Page Post Ad". My understanding is that this is really what "Boosting a Post" creates, but in a streamlined way. Coming in through the API, there is no such streamline, so the term "Boost" doesn't get used, but there is still some pretty good documentation.
I'd start with the second paragraph of this section:
https://developers.facebook.com/docs/marketing-api/buying-api/ad-units#creative

How to send direct message from Page with Attachment via Facebook Graph API?

I can manage Page Conversations (read, write) in help with Facebook Graph API. But it seems to be impossible to add any attachments to my direct messages.
https://developers.facebook.com/docs/graph-api/reference/conversation/messages
I see only "message" parameter in documentation. So, is there any way how to do it?
As #Niraj Shah mentioned above, the attachment sending feature is undocumented (for the time of this post, GraphAPI v2.12), but exists and works if you will post the source field:
PHP:
$fb =
new Facebook([
'app_id' => 'your app id',
'app_secret' => 'your app secret',
'default_graph_version' => 'v2.12',
'default_access_token' => 'your page token',
]);
$response =
$fb->post(
"/{$conversationId}/messages",
[
'message' => '',
'source' => $fb->fileToUpload($attachmentFileName),
]
);
The message field can be empty to send an attachment only.
As the documentation points out, the API only supports the message parameter. So only text can be sent in a message, and attachments are not supported. However, you can try sending the source or url parameter in your API call and see if Facebook adds it to the message (it could be a undocumented feature).

Facebook timeline wallpost doesn't work as aspected

Im using the Facebook SDK Api [PHP] to post automaticly on a timeline wall.
I created a App and requested the access_token with the following permissions: manage_pages,publish_stream,offline_access.
Im using now the access_token to post the message on a page (/PAGEID/feed) where im moderator.
The following is going OKAY and get the post working under the name of the Facebook page:
$attachment = array(
'access_token' => $accessToken,
'message' => $description,
);
$res = $facebook->api('/PAGEID/feed', 'POST', $attachment);
When Im adding some link + name AND/OR picture it will post not as wall post but as "recent message of others" with as name my own user:
$attachment = array(
'access_token' => $accessToken,
'message' => $description,
'link' => 'http://www.google.nl',
'name' => $description,
'picture' => $image,
);
How can I post full messages with link and picture on the wall self?
If I understand you correctly, you are trying to post as the Page but it's showing up as a post by you. Is that correct?
If so, I think you need to post using the Page's access token, rather than your own access token. Look at the section called "Page Access Tokens" on https://developers.facebook.com/docs/reference/api/page/. Once you get the page's access token using the technique described there, you can set it to be the token that PHP SDK uses by doing $facebook->setAccessToken(.... access token value ...). Then you can do the post and it should show up as having been created by the page.
This problem troubled me for so long. Everything seemed setup correctly but as soon as I added a link, it would post as Admin instead of Page. The answer above by triangle_man solved my problem. When using the PHP sdk, the specific line of code I needed was:
$page_token = $facebook->api("/$page_id?fields=access_token");

Post Survey on our company facebook page using PHP SDK

Our company deals with various online contests. As a part of functionality we need to post questions with fixed options to our facebook page periodically. Any idea about how to implement this using facebook api?
You can do it using graph api. The url will be {page_id}/questions. You need to pass the question as a string and options as an array.
Below code helps you to do that using facebook PHP SDK:
$question = $_POST['question'];
$options = array($_POST['option1'], $_POST['option2'], $_POST['option3']);
$api_params = array(
'question' => $question,
'options' => $options,
'allow_new_options' => false
);
$url=$facebook->api($_POST['select-page'] . '/questions', 'POST', $api_params);
Visit this link for demo and code samples

PHP Facebook wallpost tracking

I have such code to post on my friends wall from application
$args = array(
‘message’ => 'my message',
‘name’ => ‘This is my demo Facebook application!’,
‘caption’ => “Caption of the Post”,
‘link’ => $data['view_url'],
‘description’ => ‘this is a description’,
‘picture’ => $body_all["thumb"],
‘actions’ => array(array(‘name’ => ‘View’,
‘link’ => $data['view_url']))
);
$post_id = $facebook->api(“/”.user_$id.”/feed”, “post”, $args);
In array $post_id['id'] facebook returned me id of the post(like that: 100003284224348_121820851270722), and then when i wona to track it, and get some info by ID, i ask FBgraph https://graph.facebook.com/$post_id['id'] but its return false… i have publish_permission, read_streem permission in my application… what i do wrong? why i couldn`t see post in info with id on Facebook graph?
If the graph API GET request is returning false this is usually a tell tell sign that there is an access token issue at the heart of the problem. When you are making the GET request ensure you are using your applications access token.
Try it here: http://developers.facebook.com/tools/explorer/?method=GET&path=100003284224348_121820851270722
It could be a timing issue, where you get back the ID and try to use it before facebook's API can see it. Try waiting a moment and checking. Does it come up in the graph API explorer tool?