how to auto share in facebook once form is submitted - php

I have a php form for facebook for a certain media house.To fill the form, the media house facebook profile should be liked first. When the profile is liked then only the form is accessible. I have so far succeeded in that. Now when the form is successfully submitted I want to share some info as XXX applied for "YYYY" campaign automatically in the fb profile of XXX. Is it possible? I have done facebook sharing with asking permission only. Is it possible to share some info after successfully form submission without asking for permission.
Any help / suggestions are welcome. Thanks in advance.

In success page I added the following code:
Before
<?php
require_once('php-sdk/facebook.php');
$config = array(
'appId' => 'XXX',
'secret' => 'YYY',
'allowSignedRequest' => false,
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYHOST] = 2;
$message = 'Message Here;
?>
Then in body part:
<?php
if($user_id){
try(){
$ret_obj = $facebook->api('/me/feed', 'POST', array(
'link' => 'https://www.facebook.com/myrepublica',
'message' => $message,
)
);
}catch(FacebookApiException $e) {
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream',
));
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
}else{
$login_url = $facebook->getLoginUrl( array( 'scope' => 'photo_upload') );
echo 'Please login.';
}
?>
Hope this helps someone.

Related

Redirect loop when trying to publish a checkin via Facebook API

I am trying to publish a checkin using facebook api. Sometimes, it works well and posts the checkin, but mostly 99% of the times it produces the error: "This webpage has a redirect loop"
checkin.php:
<?php
require("../src/facebook.php");
// construct the object with your facebook app data
$facebook = new Facebook(array(
'appId' => 'XXXXX',
'secret' => 'XXXX',
'cookie' => false
));
$token = $facebook->getAccessToken();
//echo $token;exit())
try {
// to get the id of the currently logged in user
// if, you want you can manually set a user id here like this:
//$uid = '[FB USER ID]';
$uid = $facebook->getUser();
$facebook->setAccessToken($token);
$facebook->api('/'.$uid.'/checkins', 'POST', array(
'access_token' => $facebook->getAccessToken(),
'place' => '101697613295949',
'message' => 'Enjoying Chill Beer with Team',
'picture' => 'http://test.com/someplace.png',
'coordinates' => json_encode(array(
'latitude' => '28.541203543000023',
'longitude' => '77.15503053709995',
'tags' => 'XXXX'))
));
echo 'You are checked in';
} catch (Exception $e){
// No user found - ask the person to login
$login_url = $facebook->getLoginUrl();
header("Location: ".$login_url);
}
?>
Thanks in advance.

FB App cant post Image to page?

I recently created a Facebook App to send visualized data (image) to pages which are already belongs to same user account as The App.
Also, I set extended permissions in Permissions Pane of App. Setting Page.
Here are the codes that can send post in text format. But when I tried to send images attached it fails.
What are the possible mistakes that I can't get at the moment?
$facebook = new Facebook(array(
'appId' => MY_APP_ID,
'secret' => MY_APP_SECRET,
'fileUpload' => true,
'allowSignedRequest' => false
));
$img = "../../../tw-data/postImages/twpostimage-usd.png";
$message = 'Activity Test with Image # ' . date('d.M.Y H:i:s');
try
{
$access_token = $facebook->getAccessToken();
$params = array('access_token' => $access_token,
'source' => '#'.$img,
'message' => $message
);
$result = $facebook->api('/'. MY_PAGE_ID .'/feed','POST', $params);
print_r($result);
}
catch (FacebookApiException $e)
{
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
What Errors are you getting when you try this?
I believe the main issue you're having is that you're posting to
/my_page_id/feed
where you should be posting to
/my_page_id/photos
From the facebook docs
$response = $facebook->api(
"/me/photos",
"POST",
array (
'url' => '{image-url}',
)
);
EDIT:
I have successfully posted an image with a message using the following format.
$img = 'FILE_NAME.jpg';
$message = 'Activity Test with Image # ' . date('d.M.Y H:i:s');
$params = array('access_token' => $access_token,
'message' => 'test',
'source' => '#'.$img
);
$result = $facebook->api('/USER_ID/photos','POST', $params);
print_r($result);
Also, I set extended permissions in Permissions Pane of App. Setting
Page.
That's a common mistake. Permissions have to be requested in your scope, not in the app settings.
$params = array(
'scope' => 'publish_stream',
'redirect_uri' => 'https://www.myapp.com/post_login_page'
);
$loginUrl = $facebook->getLoginUrl($params);
Accept the permissions dialog and try again. If you want to post to the page as page, obtain a page access token.
Happy Coding!

Facebook Post link to wall error

I am trying to post a link on the facebook wall. but i got these error. please help me anyone to solve this error.
$config = array(
'appId' => 'xxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
if($user_id) {
try {
$facebook->api('/$user_id/feed','POST',
array( 'access_token' => $facebook->getAccessToken(),
'message' => 'Hello World!',
'link' => 'www.example.com'
)
);
} catch(FacebookApiException $e) {
$result = $e->getResult();
error_log(json_encode($result));
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream'
));
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
echo '<br />logout';
} else {
as the redirect_uri, so we don't
// need to specify it here.
$login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) );
echo 'Please login.';
}
{"error":{"message":"(#1) An error occured while creating the share","type":"OAuthException","code":1}}
OAuthException
(#1) An error occured while creating the share
{"error":{"message":"(#1) An error occured while creating the share","type":"OAuthException","code":1}}
OAuthException
(#1) An error occured while creating the share
Could you try to add 'type' => 'link' and see if it works? As far as I've read on Facebook forums this is actually a bug.
you are not doing anything wrong, there is a small php mistake in your code due to which your $user_id variable is not being read.
solution,replace single quote with double quote around /$user_id/feed in your code. try this it will work:
$facebook->api("/$user_id/feed",'POST',
array( 'access_token' => $facebook->getAccessToken(),
'message' => 'Hello World!',
'link' => 'www.example.com'
)
);

Facebook PHP SDK app to upload photo to page

I am trying to create a Facebook application that uploads images to a page. I can manage to upload photos to the user's photo album but not to the page's.
Here's my code:
require_once('php-sdk/facebook.php');
$config = array(
'appId' => 'XXXXXXXX',
'secret' => 'YYYYYYYYYYYYYYYY',
'fileUpload' => true,
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$photo = $_POST['image'];
$message = "Lorem ipsum";
echo $user_id;
if ($user_id) {
$ret_obj = $facebook->api('/XXXXXXXXXX/photos', 'POST', array(
'access_token' => $access_token,
'source' => '#' . $photo,
'message' => $message
)
);
} catch(FacebookApiException $e) {
$login_url = $facebook->getLoginUrl( array(
'scope' => 'user_photos,publish_stream,photo_upload'
));
echo 'Please login.';
print_r($e);
error_log($e->getType());
error_log($e->getMessage());
}
echo '<br />logout';
} else {
$login_url = $facebook->getLoginUrl( array( 'scope' =>user_photos,publish_stream,photo_upload,manage_pages' ) );
echo 'Please login.';
}
Any help would be much appreciated.
When you do:
$facebook->api('/XXXXXXXXXX/photos', 'POST', array(
... is XXXXXXXXXX your appId or the albumId? You must do the call to your album ID.
Also make sure you are using the correct $access_token. To make sure look at:
https://graph.facebook.com/yourPageID?fields=access_token&access_token=YourPersonalAccessToken
where yourPersonalAccessToken is:
A Page admin access_token for this page; The current user must be an
administrator of this page; only returned if specifically requested
via the fields URL parameter.
Your must grant to graph api manage_pages permissions.

Schedule posts on Facebook page with PHP SDK

I am trying to schedule wall posts to be added to the Facebook business page in the future.
As far as I can see Facebook does not recommend to use "offline_access" anymore.
How would you do that?
This is my code so far. It works if I am already logged into Facebook.
EDIT: Naturally I will create some code that check the schedule that I pull from the database. And use a cron job to regulary check that schedule.
require_once('src/facebook.php');
$config = array(
'appId' => 'xxxxxxx',
'secret' => 'xxxxxxx',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$page_info = $facebook->api("/PAGE_ID?fields=access_token");
try {
$ret_obj = $facebook->api('/PAGE_ID/feed', 'POST',
array (
'link' => 'http://www.example.com/',
'message' => 'This is a test',
'access_token' => $page_info['access_token']
));
} catch(FacebookApiException $e) {
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream'
));
print_r($e->getType());
print_r($e->getMessage());
}
You have to extend access_token periodically now. All other code should be working fine as previously

Categories