I need my application to publish a post on my Wall page automatically and for that use PHP-SDK, but the publication does not appear. Until the notification that the publication occurred, but it does not appear in the Wall page. In the same script, if I change the page_id by "me" the publication is made in the wall of my profile perfectly.
See code:
$fb_page_id = 'xxxxxxxxxx';
$fb_app_id = 'xxxxxxxxxxxxxxxxxxxx';
$fb_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$fb_app_url = 'https://apps.facebook.com/application/';
$fb_app_baseUrl ="http://www.mysite.com/app/application/";
$msg = array(
'message' => 'date: ' . date('Y-m-d') . ' time: ' . date('H:i')
);
//construct the message/post by posted data
$msg['message'] = 'Test';
$msg['link'] = $link;
$msg['picture'] = "http://www.mysite.com/app/facebook/icone_75_75.jpg";
$msg['name'] = $titulo;
$msg['caption'] = '';
$msg['description'] = $texto;
$msg['actions'] = array( array('name' => 'test', 'link' => 'http://www.mysite.com'));
//Create facebook application instance.
$facebook = new Facebook(array(
'appId' => $fb_app_id,
'secret' => $fb_secret
));
try {
$facebook->api('/'.$fb_page_id.'/feed', 'POST', $msg);
} catch (FacebookApiException $e) {
print_r($e);
}
thanks.
Now I created a database for storing received tokens. I used the token to post, but did not work!
Related
I use facebook-php-sdk-v3.2 to upload video from my website to a Facebook account.
This is my code:
<?php
require 'src/facebook.php';
$file = "movie.mp4";
$post_type = '/me/videos';
$post_params = array(
'access_token' => '***************************',
'title' => 'title of the image',
'source' => '#' . $file,
);
$facebook = new Facebook(array(
'appId' => '1061864210567772',
'secret' => '***************************'
));
print $facebook->getUser();
$facebook->setFileUploadSupport(true);
try{
$fbpost = $facebook->api($post_type, 'POST', $post_params);
print($fbpost);
} catch (Exception $e) {
echo $e->getMessage();
}
This question is already asked before and I saw all of them, but none of them worked for me. The output of this code is 0
$uid = $facebook->getUser();
But the error is
An active access token must be used to query information about the current user
Years ago I've developed apps for Facebook, but now seems not working old method to get user's information (id, name, etc) and post data on user's wall.
I've used like this, but for now It returns me blank screen:
require_once('FacebookAPI/Facebook.php');
$facebook = new Facebook(array(
'appId' => 'xxxxx',
'secret' => 'xxxxx',
));
$user = $facebook->getUser();
if ($user)
{
try
{
#Code below to post data on user's wall
# Photo Caption
$photoCaption = 'Just used app';
$imageUrl = ('http://example.com'); // Example URL
$link = ('http://www.facebook.com/page/app_xxxx?ref=ts');
# Post Data for Photos API
$post_data = array(
'message' => $photoCaption,
'link' => $link,
'caption' => 'Caption text'
);
$apiResponse = $facebook->api('/me/feed', 'POST', $post_data);
?>
#Code below to show app
<html>
<body bgcolor="#ffffff">
<center>
// link to my app
</center>
</body>
</html>
// Code below to get user's information (name, id)
<?php
$user_profile = $facebook->api('/me');
$coded = $_REQUEST['code'];
$name = $user_profile['name'];
$id = $user_profile['id'];
session_start();
$_SESSION['name'] = $name;
$_SESSION['id'] = $id;
}
catch (FacebookApiException $e)
{
$user = null;
error_log($e);
}
}
else
{ # Code below to get access from user
$redirectUri = 'http://www.facebook.com/mypage/app_xxxx?ref=ts';
$loginUrl = $facebook->getLoginUrl( array(
'scope' => 'publish_stream,photo_upload',
'redirect_uri' => $redirectUri
));
echo("<script>top.location.href = '" . $loginUrl . "';</script>");
}
Could someone help me out to get successfully user's name, id?
You can try this code. Hope you have downloaded latest php-sdk, you have appid,app des
include_once "php-sdk/src/facebook.php";
$app_id = 'your app id';
$facebook = new Facebook(array(
'appId' => 'your app id',
'secret' => 'your app secret'
));
$user = $facebook->getUser();
if ($user) {
$me = $facebook->api('/me');
$name= $me['name'];
$fid=$me['id'];
}
else
{
$loginUrl = "http://www.facebook.com/dialog/oauth?client_id=" .
$app_id . "&redirect_uri=" . urlencode($app_url) . "&scope=email";
echo("<script> top.location.href='" . $loginUrl . "'</script>");
}
i want for my app to post on a user's wall on behalf (for advertising e.g.). I think about creating a php script, which i will execute with a cron job on my server every week.
I have the userids in my database.
Now i want that the script gets an userid and then posts on the wall of the user. (of course if the user has got still installed the app and granted the publish stream permission)
is it possible to create a script that triggers this?
$post = $facebook->api("/$user1/feed","POST",$params); or
$post = $facebook->api("/$user2/feed","POST",$params); etc...?
Thank for advise
<?php
require 'src/facebook.php';
$app_id = 'yourappid';
$app_secret = 'yourappsecret';
$app_namespace = 'appname';
$app_url = 'https://apps.facebook.com/' . $app_namespace . '/';
$scope = 'email,publish_actions';
// Init the Facebook SDK
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
// Get the current user
$user = $facebook->getUser();
// If the user has not installed the app, redirect them to the Auth Dialog
if (!$user) {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => $scope,
'redirect_uri' => $app_url,
));
print('<script> top.location.href=\'' . $loginUrl . '\'</script>');
}
else {
try {
$params = array(
'message' => "your message",
'name' => "hello world",
'description' => "hello world",
'link' => "hello world",
'picture' => "hello world",
);
$post = $facebook->api("/$user/feed","POST",$params);
echo "";
}
catch (FacebookApiException $e) {
$result = $e->getResult();
}
}
?>
To solve your problem , I will share my code with you .
this worked for me .
You need just to to write some loop while to select all users id from your database .
<?php
//// publish as status
//// publish post to users
require_once("facebook.php");
$app_id = "xxxxxxxxxxx";
$app_secret = "xxxxxxxxxxxxxx";
$my_url = "http://t.xxxxx.net/facebook/publish.php/"; // refer number
///
$config = array();
$config['appId'] = 'xxxxxxxx';
$config['secret'] = 'xxxxxxxxxxxx';
$config['fileUpload'] = false; // optional
$facebook = new Facebook($config);
///
////
function getAccessToken() {
if ($this->accessToken !== null) {
return $this->accessToken;
}
$this->setAccessToken($this->getApplicationAccessToken());
$user_access_token = $this->getUserAccessToken();
if ($user_access_token) {
$this->setAccessToken($user_access_token);
}
return $this->accessToken;
}
function getApplicationAccessToken() {
return $this->appId.'|'.$this->appSecret;
}
/////////////////////////////// Update status Function
//xxxxxxxx
$session = $facebook->getUser();
// New SDK
$facebook->api ( array(
'method' => 'users.setStatus',
'status' => 'Hi
this new status by my app
',
'uid' => '21511',/// user_id
'session'=>$session,
) );
?>
I need to post on facebook fan page with app.
My post type is text+image.
When i post only text, i have no problem, but when i try to add a jpg it doesn't works.
Here's my code:
<pre>
<?php
require_once 'facebook.php';
// configuration
$appid = 'XXX';
$appsecret = 'YYY';
$pageId = 'ZZZ';
$msg = 'MSG MSG MSG';
$title = 'TITOLO TITOLO TITOLO';
$uri = 'http://www.google.it/';
$desc = 'Description';
$pic = 'http://sharefavoritebibleverses.com/images/bible_verses.png';
$action_name = 'AZIONE NOME';
$action_link = 'http://www.yahoo.it';
$facebook = new Facebook(array(
'appId' => $appid,
'secret' => $appsecret,
'cookie' => false,
));
$user = $facebook->getUser();
// Contact Facebook and get token
if ($user)
{
// you're logged in, and we'll get user acces token for posting on the wall
try
{
$facebook->setFileUploadSupport(true);
$page_info = $facebook->api("/$pageId?fields=access_token");
if (!empty($page_info['access_token'])) {
/*$attachment = array(
'access_token' => $page_info['access_token'],
'message' => $msg,
'name' => $title,
'picture'=>$pic
);*/
$photo_details = array(
'message' => 'message',
'access_token' => 'XYZ'
);
$photo_details['picture'] = '#'.realpath('aaa.jpg');
echo realpath('aaa.jpg');
$status = $facebook->api("/$pageId/feed", "post", $photo_details);
}
else
{
$status = 'No access token recieved';
}
}
catch (FacebookApiException $e)
{
error_log($e);
$user = null;
}
}
else
{
// you're not logged in, the application will try to log in to get a access token
header("Location:{$facebook->getLoginUrl(array('scope' => 'photo_upload,user_status,publish_stream,user_photos,manage_pages'))}");
}
echo $status;
echo "<br>CONT=".count($status);
?>
Also i need a format like second one, and not like first:
What's wrong in my code?
Thanks!
Try this:
//At the time of writing it is necessary to enable upload support in the Facebook SDK, you do this with the line:
$facebook->setFileUploadSupport(true);
//Create an album
$album_details = array(
'message'=> 'Album desc',
'name'=> 'Album name'
);
$create_album = $facebook->api('/me/albums', 'post', $album_details);
//Get album ID of the album you've just created
$album_uid = $create_album['id'];
//Upload a photo to album of ID...
$photo_details = array(
'message'=> 'Photo message'
);
$file='app.jpg'; //Example image file
$photo_details['image'] = '#' . realpath($file);
$upload_photo = $facebook->api('/'.$album_uid.'/photos', 'post', $photo_details);
Please try to make sure that $pic is the relative image location on your server and not a generic web url of the image.
Here is the code i used to post stories to facebook page wall from my website. i have installed facebook sdk like facebook.php and base_facebook.php etc files.My requirement is to post stories without logging into facebook.i have gone through many tutorials yet i couldnt find any solution for this .can you please help me in this.here is the code which i am using.
$appid = 'xxxxx'; //Application ID
$appsec = 'xxxxx'; // Application secret
$redirectUrl = 'http://test.com/admin/'; //Facebook redirects back to this page
$permissions = 'publish_stream'; // Permissions we will need
if(isset($_POST['FacebookPageID']) && strlen($_POST['FacebookPageID'])>10)
{
$_SESSION['FacebookPageID']=$_POST['FacebookPageID'];
$_SESSION['FacebookMessage']=$_POST['FacebookMessage'];
$_SESSION['FacebookTitle']=$_POST['FacebookTitle'];
}
if(!is_numeric($_SESSION['FacebookPageID']) || strlen($_SESSION['FacebookPageID'])<5)
{
die("<meta http-equiv=\"refresh\" content=\"2;URL=".$redirectUrl."\" />");
}
else
{
if(!isset($_GET["code"]))
{
$_SESSION['state'] = md5(uniqid(rand(), TRUE));
$RedirectToFacebook = "https://www.facebook.com/dialog/oauth?client_id=".$appid;
$RedirectToFacebook .="&redirect_uri=".urlencode($redirectUrl.'test.php');
die("<script type=\"text/javascript\">top.location.href='" . $RedirectToFacebook . </script>
<noscript><a href='".$RedirectToFacebook."'>Needs Permissioins</a></noscript>");
}
else
{
############## Facebook Page ID ############
$facebookPageID = $_SESSION['FacebookPageID'];
############## Wall Message ############
$facebookMessage = (empty($_SESSION['FacebookMessage']) || s trlen($_SESSION['FacebookMessage'])<5)?"Nice Facebook Wall Posting Script!":$_SESSION['FacebookMessage'];
$facebookTitle = (empty($_SESSION['FacebookTitle']) || strlen($_SESSION['FacebookTitle'])<5)?"Nice Facebook Wall Posting Script!":$_SESSION['FacebookTitle'];
if($_SESSION['state'] !="")
{
echo $_SESSION['state'];
// $siteurl=$_GET['realurl'];
$AccessTokenUrl = "https://graph.facebook.com/oauth/access_token?client_id=".$appid;
$AccessTokenUrl .="&redirect_uri=".urlencode($redirectUrl.'test.php');
$AccessTokenUrl .="&client_secret=".$appsec;
$AccessTokenUrl .="&code=".$_GET["code"];
$ReturnedString = file_get_contents($AccessTokenUrl);
$params=null;
parse_str($ReturnedString, $params);
$OurAccessToken = $params['access_token']; //access token
//---------------
require_once('src/facebook.php' ); //Include our facebook Php Sdk
$post_url = '/'.$facebookPageID.'/feed';
$facebook = new Facebook(array(
'appId' => $appid,
'secret' => $appsec,
));
//the Posting Parameters
$PostData = array(
'message' =>$facebookMessage,
'name' => $_SESSION['title'],
'caption' => "testcom",
'link' => 'from tranz',
'description' => $facebookMessage,
' picture' => "http://test/uploads/".$_SESSION['imageshare']."",
'access_token' =>$OurAccessToken,
'actions' => array(
array(
'name' => 'Saaraan',
'link' => 'http://www.saaraan.com'
)
)
);
//print_r($PostData); exit;
try {
$result = $facebook->api($post_url, 'post', $PostData);
//$result = $facebook->api('me/feed','post', $PostData);
if($result)
{
// session_destroy();
echo 'Done..';
die("<meta http-equiv=\"refresh\" content=\"2;URL=".$redirectUrl."? success=1&fbp=".$facebookPageID."\" />");
}
}
catch (Exception $e)
{
echo 'Facebook could be experiencing some problem! Try again later <br />Facebook Says: '. $e->getMessage();
}
//--------------
}
}
Facebook requires users to be authenticated to post something. No, it is not possible to post without authentication, if authentication is what you mean by "logging in."