Prompts user to publish feed with PHP SDK - facebook-graph-api

Sorry for my English, I'll try to explain my problem.
With this code I can publish feed on the wall of the users without prompts (I need extended permissions..)
$ret_obj = $facebook->api('/me/feed', 'POST',
array(
'link' => 'www.example.com',
'message' => 'Posting with the PHP SDK!'
));
But How can I use PHP SDK to prompts the user to publish as can be done with this code (using Javascript SDK)
<script src='http://connect.facebook.net/en_US/all.js'></script>
<p><a onclick='postToFeed(); return false;'>Post to Feed</a></p>
<p id='msg'></p>
<script>
FB.init({appId: "YOUR_APP_ID", status: true, cookie: true});
function postToFeed() {
// calling the API ...
var obj = {
method: 'feed',
link: 'https://developers.facebook.com/docs/reference/dialogs/',
picture: 'http://fbrell.com/f8.jpg',
name: 'Facebook Dialogs',
caption: 'Reference Documentation',
description: 'Using Dialogs to interact with users.'
};
function callback(response) {
document.getElementById('msg').innerHTML = "Post ID: " + response['post_id'];
}
FB.ui(obj, callback);
}
</script>
Thanks to all!

Seems you will need to cUrl the dialog end point to get the effect you desire.
I would suggest using a button or link and opening a new page which will cUrl the endpoint and redirect back to that page with a close tab or page button.
example endpoint link https://www.facebook.com/dialog/feed?app_id=135669679827333&link=https://developers.facebook.com/docs/reference/dialogs/&picture=http://fbrell.com/f8.jpg&name=Facebook%20Dialogs&caption=Reference%20Documentation&description=Using%20Dialogs%20to%20interact%20with%20users.&redirect_uri=http://anotherfeed.com/?pid=facebook
Refer to: https://developers.facebook.com/docs/reference/dialogs/#display
examples: under construction.
<?php
require 'facebook.php';
// Create our application instance
// (replace this with your appId and secret).
$app_id = "APP_ID";
$secret = "APP_SECRET";
$app_url = "APP_URL";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $secret,
'cookie' => true,
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
$access_token = $facebook->getAccessToken();
}
?>

Related

Create new page locations FACEBOOK API

I'm trying to create a new location for an existing page using an APP with Standard Access, but It's show me an error "Only white-listed APP can create new pages using this endpoint /{page_id}/locations".
Facebook SDK PHP
Facebook Graph API v2.9
My code:
$fb = new Facebook(['app_id' => app_idp,'app_secret' => app_secret,]);
$fb->setDefaultAccessToken(master_page_access_token);
$new_location['hours'] = array("mon_1_open"=> "10:00",
"mon_1_close"=>"19:00",
"tue_1_open"=> "10:00",
"tue_1_close"=> "19:00",
"wed_1_open"=>"10:00",
"wed_1_close"=> "19:00",
"thu_1_open"=> "10:00",
"thu_1_close"=> "19:00",
"fri_1_open"=> "10:00",
"fri_1_close"=> "19:00",
"sat_1_open"=>"10:00",
"sat_1_close"=>"19:00"
);
$location = array(
'hours'=>$new_location['hours'],
'location'=> array(
'street'=>$full_address,
'latitude' => $latitude,
'longitude'=> $longitude,
'city_id'=> $city_id,
'zip'=> $zip),
'phone' => $phone ,
'place_topics'=>array('210979565595898',
'199512783398620',
'108472109230615'),
'price_range' => '$$',
'store_location_descriptor' => $store_location_descriptor,
'store_name'=> $store_name,
'store_number'=> $store_id,
);
try {
$new_pages = $fb->post('/{page_id}/locations',$location,master_page_access_token);
}
catch (Exceptions\FacebookResponseException $exc) {
echo $exc->getMessage();
}
Error
enter image description here

Facebook API not getting response for non register app user

When I logged in by user who has created it gives return post but for others user.it is not working.
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
'cookie' => true
));
$user = $facebook->getUser();
$par = array();
$par['scope'] = "status_update user_about_me user_status,user_post,user_stream";
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
try {
// Proceed knowing you have a user who is logged in and authenticated
// Get Feed
$feed = $facebook->api('/me/feed');
// Get User Profile
$profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
Not sure what you expect, and "not working" is not a good error description, but you canĀ“t get data of users who did not authorize your App. So the code after "if ($user)" will not be called in that case.
Btw, you may want to fix your scope parameter. Some commas are missing and there is no permission called user_stream or status_update.

adding like comment issue using graph api

i have put last 3 posts in web page , if user loged in he can add comment / like , problem is if user loged in and try add like or comment he is getting an permission error #200 , if i loged in i can add like or comment (application is for me) , am getting the all permission nedded from the user , so how can i give him permission to add like / comment.
CODE :
$facebook = new Facebook(array(
'appId' => '',
'secret' => '',
'cookie' => true,
));
$user = $facebook->getUser();
if ($user) {
if (session_id()) {
} else {
session_start();
}
$access_token = $facebook->getAccessToken();
//check permissions list
$permissions_list = $facebook->api(
'/me/permissions', 'GET', array(
'access_token' => $access_token
)
);
//check if the permissions we need have been allowed by the user
//if not then redirect them again to facebook's permissions page
$permissions_needed = array('publish_stream', 'read_stream', 'manage_pages');
foreach ($permissions_needed as $perm) {
if (!isset($permissions_list['data'][0][$perm]) || $permissions_list['data'][0][$perm] != 1) {
$login_url_params = array(
'scope' => 'publish_stream,read_stream,manage_pages',
'fbconnect' => 1,
'display' => "page",
'redirect_uri' => 'http://localhost/fb/index.php',
);
$login_url = $facebook->getLoginUrl($login_url_params);
header("Location: {$login_url}");
exit();
}
}
}else {
//if not, let's redirect to the ALLOW page so we can get access
//Create a login URL using the Facebook library's getLoginUrl() method
$login_url_params = array(
'scope' => 'publish_stream,read_stream,manage_pages',
'fbconnect' => 1,
'display' => "page",
'redirect_uri'=>'http://localhost/fb/index.php',
);
$login_url = $facebook->getLoginUrl($login_url_params);
//redirect to the login URL on facebook
header("Location: {$login_url}");
exit();
}
$logoutUrl = $facebook->getLogoutUrl();
and if the user click on like button :
jQuery('ul.fb_list a').click(function(){
var comm_id = jQuery(this).attr("class");
jQuery.post('https://graph.facebook.com/'+comm_id+'/likes/',{
access_token : "<?php echo $access_token ?>"
});
});
Double check and make sure your variable comm_id is formated with both the user id of the person who posted the post, and then the id of the post itself, with an underscore in between, like this - USERID_POSTID. This is how facebook gives it you if you call to the graph api
$post_url = "https://graph.facebook.com/" . $user_id . "/posts?access_token=". urlencode($access_token);
Not sure if you're getting comm_id from another source or not. Also noticed in your code
access_token : "<?php echo $access_token ?>"
You forgot a semi-colon after the echo call. Should be this
access_token : "<?php echo $access_token; ?>"
Hope this helps. I see you're using a lot of jquery to do the like functionality. I like to stick w/ server side code for stuff like this, I feel that it's more stable for some reason.
This is how I did it. I have a like button like this -
echo '<div id="like_container-'.$i.'">
<div id="like_count">'.$num_likes.'</div>'
<div class="unliked"></div>
</div>';
and fb_Like() is an ajax call, something like this -
function fb_Like(post_id, token, num_likes, id){
$.ajax({
type: "GET",
url: "likepost.php",
data: 'id='+post_id+'&token='+token+'&likes='+num_likes,
success: function(html)
{
$("#like_container-"+id).empty().html(html);
}
});
}
And the likepost.php page is a script similar to the one on this page
Like a Facebook Post externally using Graph Api - example
This worked really well for me. It also let's me update the number of likes that the post has on the front end right above the like button if a like has been made. Good luck!
UPDATE
If you want to check if the user already likes a post, it's pretty simple w/ the facebook graph api
//Create Post Url
$post_url = "https://graph.facebook.com/" . $Page/User_id . "/posts?access_token=". urlencode($access_token);
//Get Json Contents
$resp = file_get_contents($post_url,0,null,null);
//Store Post Entries as Array
$the_posts = json_decode($resp, true);
foreach ($the_posts['data'] as $postdata) {
foreach ($postdata['likes']['data'] as $like){
if($like['id']==$user){
$liked=1;
}else{continue;}
}
if($liked==1){
//do something
}
}
This assumes that you already have a facebook user id for the logged in user, in this example, stored in the variable $user.

How do I get Permission to post to Facebook Page Wall

After reading the developers.facebook.com for the past few hours I just can't seem to figure out how to get my Facebook Page to allow permission for me to post to its wall from a Django Website?
you must give related permissions to your app(publish_stream,manage pages)
then you can post to page as page
posting to page as page if you have page's access_token can be like this:
include_once ('src/facebook.php');/// include sdk
////// config The sdk
# $facebook = new Facebook(array(
'appId' => 'XXXXXXX',
'secret' => 'XXXXXXXXXXXXXX',
));
try{
$post=$facebook->api('PAGE_ID/feed/','POST',array(
'message' => '$message',
'link'=>'http://apps.facebook.com/xxxxxxx/link.php?link=',
'picture'=> 'XXXXXXXXXX',
'name'=>'XXXXXX',
'description'=>'yes',
'access_token'=>'PAGE ACCESS TOKEN'
));
}
catch(FacebookApiException $e) {
echo $e->getType();
echo '<br />';
echo $e->getMessage();
}
& if you dont have access token (you need above permissions again) i think this can help you
include_once ('src/facebook.php');/// include sdk
////// config The sdk
# $facebook = new Facebook(array(
'appId' => 'XXXXXXX',
'secret' => 'XXXXXXXXXXXXXX',
));
$facebook->destroySession();
try{
$post=$facebook->api('PAGE ID/feed/','POST',array(
'message' => '$message',
'link'=>'http://apps.facebook.com/xxxxxxx/link.php?link=',
'picture'=> 'XXXXXXXXXX',
'name'=>'XXXXXX',
'description'=>'yes',
));
}
catch(FacebookApiException $e) {
echo $e->getType();
echo '<br />';
echo $e->getMessage();
}
to learn how to get page's access_token check this link up:
https://developers.facebook.com/tools/explorer/?method=GET&path=me%2Faccounts

I want to post from my site on my fanpage

I have create an app in facebook and, between php sdk, i can post the article of my site on my profile.
Now, i want to post my article in my fanpage and not in my prfile. how i can do this?
This is the code that i use, not much different from the example in facebook developers:
// Remember to copy files from the SDK's src/ directory to a
// directory in your application on the server, such as php-sdk/
require_once('facebook.php');
$config = array(
'appId' => 'xxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxx',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<?
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$access_token = $facebook->getAccessToken();
$page_id='xxxxxxxxxxxxxx';
$ret_obj = $facebook->api('/me/feed', 'POST',
array(
'link' => $link,
'message' => $titolo,
'picture' => 'http://xxxxxxxxxxxxxxxxxx',
'name' => 'Ecosport',
'access_token' => $access_token
));
echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream'
));
header("location:$login_url");
error_log($e->getType());
error_log($e->getMessage());
}
// Give the user a logout link
$id_articolo=$_GET['id_articolo'];
header("location:xxxxxxxxxx");
/*echo '<br />logout';*/
} else {
// No user, so print a link for the user to login
// To post to a user's wall, we need publish_stream permission
// We'll use the current URL as the redirect_uri, so we don't
// need to specify it here.
$login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) );
header("location:$login_url");
/*echo 'Please login.';*/
}
?>
Oviusly the "xxxx" are used for privacy. I tied to change this " $facebook->api('/me/feed',...... " into this " $facebook->api('/$page_id/feed',....... " but nothing. I can't to post my article in my fanpage.
Can you help me?
Thank you very much.
You need to ask for the manage_pages and publish_stream permissions. Then call to /me/accounts to get a list of accounts (aka pages) that the user administers. Each account in that list of accounts will have an access token associated with it. That access token is special and will need to be used to new up an instance of the facebook api. Once you do that, posting to the page wall is the same as posting to a user's wall (i.e. /me/feed)