I'm using the following to post a message on my Facebook page:
$attachment = array(
'access_token' => $access_token,
'message' => 'This is a test Message 4:',
'name' => "This is a test Name 4",
'link' => "http://slashdot.org/",
'description' => "This is a test Description 4"
);
$ret_code=$facebook->api('/me/feed', 'POST', $attachment);
This works great.
How do I delete the same post using the facebook GRAPH api? I read the docs and it says to issue a POST like:
https://graph.facebook.com/COMMENT_ID?method=delete
To test I set this up in a simple form with submit button, POSTing the data to https://graph.facebook.com/COMMENT_ID?method=delete (substituting COMMENT_ID fro the 11111111111_111111111111 id returned from the original publish call. This returns "This API call requires a valid app_id".
What is the correct way to issue a DELETE command?
Since you are using the php-sdk you just issue this call:
$facebook->api("/COMMENT_ID","DELETE");
You can use the following code:
Http::post('https://graph.facebook.com/'.$fb_action_id, array('method'=>'delete', 'access_token'=>$your_app_access_token));
This post will return a boolean value, true if successed and false if failed.
Its been discussed here Facebook SDK and Graph API Comment Deleting Error
you need to pass the access token too. You can delete all the milestones of a page like follows:
$milestones = $facebook->api('/PAGE_ID/milestones');
foreach($milestones[data] as $milestone)
{
echo $milestone['id'];
$args = array(
'access_token' => $pages_access_token
);
$deleted = $facebook->api($milestone['id'],"delete",$args);
if($deleted)
{
echo " <font color=\"green\">OK</font><br>";
}
else
{
echo " <font color=\"red\">ERR</font><br>";
}
}
Related
I'm trying to tag user(s) inside page post comments.
$fb_id = 'facebook_user_id_here';
$fb_name = 'facebook_user_name_and_surname_here';
$request = new FacebookRequest(
$this->session,
'POST',
$comment,
array(
'access_token' => $this->page_access_token, //it is access token of my page
'message => "Text here #[".$fb_id.":1:".$fb_name."]"
)
);
It shows only name and surname without Fb profile URL.
Like CBroe mentions in his comment, at this time this is not possible using the API.
For reference, this is called "mentioning" or "mention tagging", and personal profiles can only do this in messages on open graph actions (docs).
Pages can also mention other pages in comments. (docs)
I'm making a facebook app which will show a user's friends, then the user will choose a friend and the app will send that friend an apprequest with PHP.
With that apprequest I also will send the user's id, so that when the receiver plays the app, I will know that it will be thanks to that user.
To get a user's friends I'm using the following code
$friends_array = $facebook->api('/me/friends');
(I know this will only return a user's friends who have already accepted the app's permissions, so I'm using 2 test accounts which are friends)
To send the apprequest
If I use the following
$param = array(
'message' => 'Check out the latest update',
'data' => 'some_data_string',
"to" => $_POST["friend_id"],
"method" => "POST",
'access_token' => $facebook->getAccessToken(),
);
$tmp = $facebook->api("/me/apprequests", "POST", $param);
It is executed, but nothing is received.
If I use the following
$param = array(
'message' => 'Check out the latest update',
'data' => 'some_data_string',
'access_token' => $facebook->getAccessToken(),
);
$tmp = $facebook->api("/".$_POST["friend_id"]."/apprequests", "POST", $param);
I get the following error "Fatal error: Uncaught OAuthException: (#2) Failed to create any app request thrown ..."
The app is public and available to all users.
Do I need maybe to submit the app for any special permissions in order to post an apprequest?
Also if you have any other suggestion to have the desired results I'm referring to at the beginning, please reply.
Now I posting a single photo to wall like this:
$response = $facebook->api("/$group_id/photos", "POST", array(
'access_token=' => $access_token,
'message' => 'This is a test message',
'url' => 'http://d24w6bsrhbeh9d.cloudfront.net/photo/agydwb6_460s.jpg',
)
);
It works fine, but can I somehow post a multiple photos, something like this:
You can now publish multiple images in a single post to your feed or page:
For each photo in the story, upload it unpublished using the {user-id}/photos endpoint with the argument published=false.
You'll get an ID for each photo you upload like this:
{
"id": "10153677042736789"
}
Publish a multi-photo story using the {user-id}/feed endpoint and using the ids returned by uploading a photo
$response = $facebook->api("/me/feed", 'POST',
array(
'access_token=' => $access_token,
'message' => 'Testing multi-photo post!',
'attached_media[0]' => '{"media_fbid":"1002088839996"}',
'attached_media[1]' => '{"media_fbid":"1002088840149"}'
)
);
Source: Publishing a multi-photo story
You can make batch requests as mentioned here: https://stackoverflow.com/a/11025457/1343690
But its simple to loop through your images and publish them directly.
foreach($photos as $photo)
{
//publish photo
}
Edit: (regarding grouping of photos on wall)
This grouping is done by facebook automatically if some photos are uploaded into the same album.
Currently you cannot create an album in a group via Graph API - it is not supported (as of now), see this bug.
But you can do this - create an album manually, then get the album_id by-
\GET /{group-id}/albums, then use the the code with album_id instead of group_id-
foreach($photos as $photo){
$facebook->api("/{album-id}/photos", "POST", array(
'access_token=' => $access_token,
'name' => 'This is a test message',
'url' => $photo
)
);
}
I've tested it, see the result-
Actually you can upload a multi story photo(I did it using Graph Api and PHP) but the problem comes if you need scheduled this post.Your post is schedule but also it shows on the page's feed.
P.S. I'm using Graph Api v2.9
PHP Code
$endpoint = "/".$page_id."/photos";
foreach ($multiple_photos as $file_url):
array_push($photos, $fb->request('POST',$endpoint,['url' =>$file_url,'published' => FALSE,]));
endforeach;
$uploaded_photos = $fb->sendBatchRequest($photos, $page_access_token);
foreach ($uploaded_photos as $photo):
array_push($data_post['attached_media'], '{"media_fbid":"'.$photo->getDecodedBody()['id'].'"}');
endforeach;
$data_post['message'] = $linkData['caption'];
$data_post['published'] = FALSE;
$data_post['scheduled_publish_time'] = $scheduled_publish_time;
$response = $fb->sendRequest('POST', "/".$page_id."/feed", $data_post, $page_access_token);
$post_id = $cresponse->getGraphNode()['id'];
You will need to upload each photo first with published state to false, and then use the ID's of the unpublished photos to the /me/feed endpoint to schedule the photo. The schedule needs to be within the 24 hours from the time the photos are uploaded as facebook deletes all unpublished photos in 24 hours.
Ref:
https://developers.facebook.com/docs/graph-api/photo-uploads/
There is no way to publish more than one photo in the same graph API call.
See documentation: https://developers.facebook.com/docs/graph-api/reference/user/photos
I have php code that posting in the wall of the user that use in my app. But in the post write
"post by _" (My app name).
Code:
$fql_info = $facebook->api($fql_query);
if($fql_info[0][offline_access] == 1)
{
$attachment = array(
'message' => "",
'name' => u("ghfhgfhfgh"),
'description' => u("fghgfhgfh"),
'link' => LINK,
'picture'=> "http://gfhgfhgfhfh"
);
$facebook->api('/'.$f[uid].'/feed', 'POST', $attachment);
$abc.=$f[uid]."\n";
echo $f[uid]."-".$fql_info[0][offline_access]."\n";
}
I want to know how I can to post but without my app name. User as poster itself, not the application has released it.
is it possible?
I guess it's not possible. The app name will appear in the post.
Sorry to say but i don't think this is possible . Facebook would not allow us to do that , it will always say Posted via : [Your APP name]
I am trying in vain to post to a Facebook fan page wall as admin. I want to post new entries with pictures, link, description etc on my fan page wall. This is integrated into my custom cms so that whenever a story is updated on my website, it automatically posts the same on the fan page wall.
The problem is that whenever I post, the post does not appear as posted by admin. It appears as posts by others on the right side of the timeline.
How do I go around the following code so that it posts as admin on my fan page wall? I been looking at it for two days, checked similar stuff on the web and so, but I still cant seem to crack it. Help will be appreciated.
$page_id = 'xxxxxxxxxx';
$article_link = "http://xyz.com/articles/headlines/title-one/";
$result = $facebook->api("/me/accounts");
foreach($result["data"] as $page) {
if($page["id"] == $page_id) {
$page_access_token = $page["access_token"];
break;
}
}
$og_title = "Sample title";
$og_image = "http://xyz.com/images/articles/919161344090182.jpg";
$msg = "some description of the article";
$feed_array = array(
'access_token' => "$page_access_token",
'message' => "$msg",
'picture' => "$og_image",
'link' => "$article_link",
'name' => "$og_title",
'caption' => "$og_title"
);
try {
$page_post = $facebook->api("/$page_id/feed","post",$feed_array);
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
If you want to post as the page - that means that the post will appear as if the page posted an update as it appears here :
Then you will have to use a valid page access token. There is information on how to obtain this kind of token in the Facebook Authentication Documentation. It looks like you already have a token in your code - you can test it using the Facebook Debugger and see if it is still valid.
Another thing you may want to try, instead of appending the token to be one of the parameters of the post array, you could use the setAccessToken() function of the PHP SDK. More details can be found here - https://developers.facebook.com/docs/reference/php/facebook-setAccessToken/
$facebook->setAccessToken($new_access_token);
As a final BTW note, when you specify elements of an array, unless you are wanting to specifically add the quote character, there is no need to wrap the values in quotes.
$feed_array = array(
'access_token' => $page_access_token,
'message' => $msg,
'picture' => $og_image,
...
);
If you want to explicitly add quotes to the values you would have to do something like this -
$feed_array = array(
'string_value' => '"'.$value.'"',
...
);