Facebook SDK Auto Post #100 - php

I have problem with Facebook SDK auto post to my Facebook business Page.
It is working code but now it's showing me error
"(#100) Only owners of the URL have the ability to specify the picture, name, thumbnail or description params. FacebookApiException"
I create cakePHP script to auto post Facebook to my Page. I create new App -> I took App Id and Secret ID. I created Access Token with long time expired. Of course I verifed domain for my Page with bussiness acount on FB.
What is wrong...? I don't have any idea!
please find my code
$config = array();
$config['appId'] = '16xxxxxxxxxxxx';
$config['secret'] = 'a24faxxxxxxxxxxxxxxxxxxxxx';
$config['fileUpload'] = false; // optional
$link = SITE_URL.'/PostAds/details/'.base64_encode($post_id);
$name = $this->request->data['title'];
$price = $this->request->data['price'];
$city = $this->request->data['city'];
$fb = new \Facebook($config);
$img_link = webroot.'/uploads/postAd/'.$image_name;
$params = array(
"access_token" => 'EAACZA18CF5TcBAL0E4XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
"message" => $name.' - '.$city.' Price:'.$price,
"link" => $link,
"picture" => $img_link,
"name" => $name,
"description" => $name.' '.$price,
"published" => true
);
try {
$ret = $fb->api('/page-id/feed', 'POST', $params);
echo 'Successfully posted to Facebook';
} catch(Exception $e) {
echo $e->getMessage();
}

Related

Create Facebook Event for group page using a SQL database and post using graph api

I have been trying to post to an event that has already been created in a mySQL database and use facebook's graph api to post it to a group page. I am able to post an array created right in the code, but I would like to post an array that I am pulling in from a POST command. Here is what I have.
public function get_event()
{
require_once 'application/php-sdk/facebook.php';
$config = array(
'appId' => '1407968509465242',
'secret' => '***********************',
'cookie' => false,
'fileUpload' => true
);
$id = $_POST['eventsDrp'];
$this->load->model('mdl_facebook_event');
$output = $this->mdl_facebook_event->get_event($id);
print_r ($output);
$facebook = new Facebook($config);
// Set the current access token to be a long-lived token.
$facebook->setExtendedAccessToken();
$access_token = $facebook->getAccessToken();
$page_id = '594922100555123';
$page_access_token = '*******************';
// Declare the variables we'll use to demonstrate
// the new event-management APIs
$event_id = 0;
$event_name = "New Event API Test Event";
$event_start = '2012-04-24T22:01:00+0000';
$event_privacy = "SECRET"; // We'll make it secret so we don't annoy folks.
// We'll create an event in this example.
// We'll need create_event permission for this.
$params = array(
'name' => $event_name,
'start_time' => $event_start,
'privacy_type' => $event_privacy,
'access_token' => $page_access_token,
'page_id' => $page_id //where $page_id is the ID of the page you are managing
);
// Create an event
$ret_obj = $facebook->api("/$page_id/events", 'POST', $output);
if(isset($ret_obj['id'])) {
// Success
$event_id = $ret_obj['id'];
echo ('Event ID: ' . $event_id);
} else {
echo ("Couldn't create event.");
}
// Convenience method to print simple pre-formatted text.
function printMsg($msg) {
echo "<pre>$msg</pre>";
}
I am trying to post the array from $output. I have tried to post from $params and everything works fine. When I try and post from $output I get Fatal error:
Uncaught OAuthException: Invalid token: "594922100555123". An ID has already been specified.

Facebook PHP API post to wall falls under "Recent Posts by Others"

I want to post a message to the wall of a Facebook Page. I am the admin of the app and the page used in this code, I already gave permissions needed for my app to be able to post on my page, it works when I use only the field "message", like this:
$message = array(
'message' => "Test2",<br>
);
$result = $fb->api('/411895472189524/feed','POST',$message);
The code above posts to my page wall and the post is made "from" the page itself, just like if I would do it manually from facebook. This is working great.
But when I try to add more fields like "link" or "picture" or "description" the post goes in the "Recent Posts by Others on TEST Jojo Page" and the post is now made from my personnal account (Joelle Landrie) instead of from the page itself. See code below.
$message = array(
'message' => "Test2",
'picture' => "http://www.cleanpopo.com/uploads/1/3/1/5/13154615/245431315.jpg",
'description' => "This is a test description",
'link' => "google.com"
);
$result = $fb->api('/411895472189524/feed','POST',$message);
See: https://www.facebook.com/pages/TEST-Jojo-Page/411895472189524
The link field seems to be causing problem, I can get a successful post on my page using the message, picture and description field. Only this is useless to me, I need my post to have a link.
SOLUTION
Thanks to Shadowfax who asked if I was using the "page_access_token". I was not. I started looking on the web how to get this token, added it to my code and now it works great!!
The Final Code
$appId = 'YOUR APP ID';
$secret = 'YOUR SECRET';
$returnurl = 'http://www.yoursite.com';
$permissions = 'manage_pages, publish_stream, offline_access';
$fb = new Facebook(array('appId'=>$appId, 'secret'=>$secret));
$fbuser = $fb->getUser();
if($fbuser){
$page_id = "YOUR PAGE ID";
$page_access_token = "";
$result = $fb->api("/me/accounts");
// loop trough all your pages and find the right one
if( !empty($result['data']) )
{
foreach($result["data"] as $page)
{
if($page["id"] == $page_id)
{
$page_access_token = $page["access_token"];
break;
}
}
}
else
{
echo "AN ERROR OCCURED: could not get the access_token. Please verify the page ID ".$page_id." exists.";
}
// set the facebook active facebook access token as the one we just fetch
$fb->setAccessToken($page_access_token);
// Now try to post on page's wall
try{
$message = array(
'message' => "YOUR MESSAGE",
'picture' => "YOUR PICTURE",
'description' => "YOUR DESCRIPTION",
'link' => "YOUR LINK"
);
$result = $fb->api('/'.$page_id.'/feed','POST',$message);
if($result){
echo 'Successfully posted to Facebook Wall...';
}
}catch(FacebookApiException $e){
echo $e->getMessage();
}
}else{
$fbloginurl = $fb->getLoginUrl(array('redirect-uri'=>$returnurl, 'scope'=>$permissions));
echo 'Login with Facebook';
}
Just posting the answer as answer.
When posting as a page, you need to get manage_pages permission, then get the desired page's access_token via /me/accounts API call and use that token to make the /{page_id}/feed POST call.
Flames, the original poster, managed to do this and posted his solution edited in the question itself. I just pasting it here and making it Community Wiki
$appId = 'YOUR APP ID';
$secret = 'YOUR SECRET';
$returnurl = 'http://www.yoursite.com';
$permissions = 'manage_pages, publish_stream, offline_access';
$fb = new Facebook(array('appId'=>$appId, 'secret'=>$secret));
$fbuser = $fb->getUser();
if($fbuser){
$page_id = "YOUR PAGE ID";
$page_access_token = "";
$result = $fb->api("/me/accounts");
// loop trough all your pages and find the right one
if( !empty($result['data']) )
{
foreach($result["data"] as $page)
{
if($page["id"] == $page_id)
{
$page_access_token = $page["access_token"];
break;
}
}
}
else
{
echo "AN ERROR OCCURED: could not get the access_token. Please verify the page ID ".$page_id." exists.";
}
// set the facebook active facebook access token as the one we just fetch
$fb->setAccessToken($page_access_token);
// Now try to post on page's wall
try{
$message = array(
'message' => "YOUR MESSAGE",
'picture' => "YOUR PICTURE",
'description' => "YOUR DESCRIPTION",
'link' => "YOUR LINK"
);
$result = $fb->api('/'.$page_id.'/feed','POST',$message);
if($result){
echo 'Successfully posted to Facebook Wall...';
}
}catch(FacebookApiException $e){
echo $e->getMessage();
}
}else{
$fbloginurl = $fb->getLoginUrl(array('redirect-uri'=>$returnurl, 'scope'=>$permissions));
echo 'Login with Facebook';
}

How to post to a facebook group wall in offline mode using graph API? (I am a member of group)

I am trying to post a status update on the wall of a group which I am a member of. Here is the code I am using
<?php
require 'facebook-php-sdk/src/facebook.php';
$appId = 'xxxxxxxxxxxxxxxx';
$appSecret = 'xxxxxxxxxxxxxxxx';
$extended_access_token = 'xxxxxxxxxxxxxxxxxxxxx';
$facebook = new Facebook(array('appId' => $appId, 'secret' => $appSecret));
$msg_body = array(
'message' => 'Good evening',
'type' => 'status',
'access_token' => $extended_access_token,
);
$groups = array(
'Group name' => '1234567',
);
foreach($groups as $group_name => $group_id){
try {
$post_url = "/$id/feed";
$postResult = $facebook->api($post_url, 'post', $msg_body );
print_r($postResult);
} catch (FacebookApiException $e) {
echo $e;
}
}
?>
If I login to fb via browser and hit this page in a new tab, the message is getting posted to the group wall. But If I am not logged into facebook, then if I hit this page, no message is getting posted and I am getting an error message
OAuthException: (#200) The user hasn't authorized the application to perform this action
How can I post to this group via offline mode? Searched a lot for this, could not find any useful info.
you need to have the permissions from the group owner
Change $id to $group_id and you'll be fine.
you ned to get apps this permession : publish_actions,publish_stream,user_groups,

How to post on group wall of facebook

I want to post on group wall. I have permission of "user_groups" and "publish_stream". and also access_token.
and here is my code:
try{
$statusUpdate = $this->facebook->api('/'.$group_id.'/feed', 'post',
array(
'name' => stripslashes($productname),
//'caption'=>$caption,
'message' => $message,
'description' => $description,
'picture' => $picture,
'link'=>$link));
$postid = $statusUpdate['id']; // return id
} catch (Exception $e){
$postid = 0;
}
return $postid; // return id
}
When I run this code I get a return id which was the page id. but nothing post on my group wall. how to solve this?
This code works for me to post to a facebook page.
Get FB page access token ($value['access_token']) and its page id ($value['id'])
Populate the $params array
use the FB API to post your message 3.
This is a snippet
foreach ($pages as $key => $value) {
$params = array(
'access_token' => $value['access_token'],
'message' => $message
);
// ask facebook api to post the message to the selected page
$facebook->api()->api( "/" . $value['id'] . "/feed", 'POST', $params );
}

facebook graph api - post large image

I finally got facebooks graph api to post messages on my fan PAGE as page
How do i get it to post large images as a post, not as a link?
'source' => $photo seems to create a thumbnail
this is what i have so far
<?php
$page_id = 'YOUR-PAGE-ID';
$message = "I'm a Page!";
$photo = "http://www.urlToMyImage.com/pic.jpg";
require '../src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'YOUR-APP-ID',
'secret' => 'YOUR-SECRET-ID',
));
$user = $facebook->getUser();
if ($user) {
try {
$page_info = $facebook->api("/$page_id/?fields=access_token");
if( !empty($page_info['access_token']) ) {
$facebook->setFileUploadSupport(true); // very important
$args = array(
'access_token' => $page_info['access_token'],
'message' => $message,
'source' => $photo
);
$post_id = $facebook->api("/$page_id/feed","post",$args);
}
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl(array( 'next' => 'http://mydomain.com/logout_page.php' ));
} else {
$loginUrl = $facebook->getLoginUrl(array('scope'=>'manage_pages,publish_stream'));
}
?>
The problem here is that you are in actual fact not posting a photo. What you are doing is posting a link to that photo so what you see is indeed a thumbnail preview image that Facebook retrieved from that URL.
What you'll want to do is provide a full path to a file on your server prefixed with the # symbol. The topic has been discussed on the site quite a bit so I'll just point you in the direction of a canonical post dealing with uploading of images to Facebook with the PHP SDK
Upload Photo To Album with Facebook's Graph API
The code looks like this -
$facebook->setFileUploadSupport(true);
$params = array('message' => 'Photo Message');
$params['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/me/photos', 'post', $params);

Categories