Facebook stream.publish and outdated template bundles? - php

I haven't tried messing with the facebook PHP API for months. Since template bundles are apparently now defunct, how can I publish a story into my users news feed for their friends? I've also already requested permissions.
Edit: The issue seems to arise from requested permissions not being set for the user when They are granted.
So far I have this
$appapikey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$appsecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$facebook = new Facebook($appapikey, $appsecret);
$fb_user = $facebook->require_login();
try {
$facebook->api_client->feed_publishUserAction();
} catch(Exception $e) { }
Edit: I've looked through the facebook "api documentation" multiple times it's just not clear to me. I can't tell what's actually deprecated or not. They link to tutorials 2-3 years old!
If you have a problem with your iframe application reloading over and over and over try using
$facebook->require_frame()

Have you already looked up the topic on the facebook wiki? http://wiki.developers.facebook.com/index.php/Stream.publish
There's a nice example which should help you out. If not, you'll have to describe your problem more accurate.
EDIT: You can check and request for the permissions like this (and also request them)
function check_perms() {
global $facebook, $uid;
$data = $facebook->api_client->fql_query( "SELECT uid, publish_stream FROM permissions WHERE uid = " . $uid );
if( $data[0]['publish_stream'] != true ) {
echo '<br /><p>No \'publish_stream\' permissons found!<br />';
echo '<fb:prompt-permission perms="publish_stream"> Allow me to publish to your wall (*click*) </fb:prompt-permission>';
echo '<br />You\'ll have to refresh the page to continue.</p>';
die();
}
}

<?php
$message ="Your Message";
$attachment = array(
'name' => 'Application Name or message',
'href' => 'http://apps.facebook.com/tshirtquote',
'description' => 'Choose/Write your T-shirt Quote, Get A Tshirt Free printed with your Favorite Quote',
'media' => array(array('type' => 'image', 'src' => 'http://linkdoo.com/tshirtquote/images/tshirt1.JPG', 'href' => 'http://apps.facebook.com/tshirtquote/')),
);
$action_links = array( array('text' => 'WriteYourTShirtQuote', 'href' => 'http://apps.facebook.com/tshirtquote'));
$attachment = json_encode($attachment);
$action_links = json_encode($action_links);
$message = json_encode($message);
?>
<script>
var attachment = <?= $attachment ?>;
var message = <?= $message ?>;
var action_links = <?= $action_links ?>;
Facebook.streamPublish(message,attachment,action_links);
</script>
Use above script , It is most easy way to publish

Related

Facebook SDK Auto Post #100

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();
}

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,

Posts scheduled via Graph API frequently do not get published

Hi I'm using facebook php sdk to make posts to my fanpage. I am attempting to schedule these posts to the future. I am running into some problems though. Here is my code
<?php
// This code is just a snippet of the example.php script
// from the PHP-SDK <https://github.com/facebook/facebook-php-sdk/blob/master/examples/example.php>
require_once('facebookphp/src/facebook.php');
$app_id = "xxxxx";
$app_secret = "xxxxxx";
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'fileUpload' => true,
));
// Get User ID
$user = $facebook->getUser();
var_dump($user);
if ($user) {
try {
$page_id = 'xxxx';
$album_id = 'xxxxx';
$page_info = $facebook->api("/$page_id?fields=access_token");
if( !empty($page_info['access_token']) ) {
$args = array(
'access_token' => $page_info['access_token'],
'scheduled_publish_time' => "1361642425", #an example timestamp
'message' => "test post",
'source' => "#" . "/path/to/photo.jpg",
'published' => "0",
);
$post_id = $facebook->api("/$album_id/photos","post",$args);
#echo $post_id;
} else {
$permissions = $facebook->api("/me/permissions");
if( !array_key_exists('publish_stream', $permissions['data'][0]) ||
!array_key_exists('manage_pages', $permissions['data'][0])) {
// We don't have one of the permissions
// Alert the admin or ask for the permission!
header( "Location: " . $facebook->getLoginUrl(array("scope" => "publish_stream, manage_pages")) );
}
}
} catch (FacebookApiException $e) {
var_dump($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
echo 'logout';
} else {
$loginUrl = $facebook->getLoginUrl(array('scope'=>'manage_pages,publish_stream'));
echo 'login';
}
// ... rest of your code
?>
This code posts a photo to my facebook page scheduled into the future perfectly, except when the schedule time comes to pass the photo is not published. In the activity log the photo remains in the 'scheduled posts' section with the error 'Sorry, something went wrong publishing this scheduled post'
I suspected this was because of the parameter: 'published' => "0",
If I remove this parameter or set it to 1 then the post is not made at all and I get the error 'You cannot specify a scheduled publish time on a published post'
Scheduling the post with the above code, works perfect work me. I just tried and scheduled the post with 11 minutes later and I got the notification after 11 minutes and photo was published on the said album.
Actually its some kinda facebook bug.
Just go to each post, click on 'Reschedule' and adjust the time by a 15 minutes (or however much you want). For some reason this resets them individually and everything goes back to normal, the posts will once again post themselves according to schedule.
I know this is a tedious way to fix something which Facebook should fix on their own, but it works.

Facebook Php image in text message

On my page I want post feeds with an image in the text of message and not like a link/url, is possible add to text message something like htmltags or bbcode?
$msg = "<img src=\"urlimg or facebook\" >\nMy text here";
$args = array(
'message' => $mgs,
);
$myfeed = $facebook->api($pageid . '/feed', 'post', $args);
update:
i've found a solution but if i post 2 times in a row they will be groupped in the same box in timeline
$args = array(
'message' => $msg,
'image' => '#'.$path,
'aid' => $album_id,
'access_token' => $token
);
$photo = $facebook->api($album_id . '/photos', 'post', $args);
exist a setting to stop auto-group that? or there is another way to post it like feed with image?
You can't publish an image in the middle of the text message, Facebook do not allow it.
But you can attach an image to the message, it will appear on the left of the message in this way:
$msg = "My text here";
imgUrl = "http://urltotheimage.com/path/image.jpg";
$args = array(
'message' => $mgs,
'picture' => $imgUrl
);
$myfeed = $facebook->api($pageid . '/feed', 'post', $args);
So I spent 30 seconds searching around the PHP Facebook API, which really is what you should be doing, and found the following example:
<?
// 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('php-sdk/facebook.php');
$config = array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
'fileUpload' => true,
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$photo = './mypic.png'; // Path to the photo on the local filesystem
$message = 'Photo upload via the PHP SDK!';
?>
<html>
<head></head>
<body>
<?
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 {
// Upload to a user's profile. The photo will be in the
// first album in the profile. You can also upload to
// a specific album by using /ALBUM_ID as the path
$ret_obj = $facebook->api('/me/photos', 'POST', array(
'source' => '#' . $photo,
'message' => $message,
)
);
echo '<pre>Photo 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' => 'photo_upload'
));
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
echo '<br />logout';
} else {
// No user, print a link for the user to login
// To upload a photo to a user's wall, we need photo_upload 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' => 'photo_upload') );
echo 'Please login.';
}
?>
</body>
</html>
Take note of the $config variable values and the $facebook->api() call.

PHP SDK for Facebook: Uploading an Image for an Event created using the Graph API

Can anyone shed some light on my problem?
<?php
$config = array();
$config['appId'] = "foo";
$config['secret'] = "bar";
$config['cookie'] = true;
$config['fileUpload'] = true;
$facebook = new Facebook($config);
$eventParams = array(
"privacy_type" => $this->request->data['Event']['privacy'],
"name" => $this->request->data['Event']['event'],
"description" => $this->request->data['Event']['details'],
"start_time" => $this->request->data['Event']['when'],
"country" => "NZ"
);
//around 300x300 pixels
//I have set the permissions to Everyone
$imgpath = "C:\\Yes\\Windows\\Path\\Photo_for_the_event_app.jpg";
$eventParams["#file.jpg"] = "#".$imgpath;
$fbEvent = $facebook->api("me/events", "POST", $eventParams);
var_dump($fbEvent); //I get the event id
I also have this in my "scope" when the user is asked to Allow the app to post on his behalf: user_about_me,email,publish_stream,create_event,photo_upload
This works. It creates the event with all the details I have specified. EXCEPT for the event image.
I have been to most of Stackoverflow posts related to my problem but all of them are not working for me. (EG: https://stackoverflow.com/a/4245260/66767)
I also do not get any error.
Any ideas?
THanks!
Yes, the idea is that Facebook has bug.
I have problems with event picture as well. The topic in tracker and this is my post on stackOverflow
Btw, you can try this code after creating the event ->
public function uploadFacebookEventPicture($fullPath, $eventId) {
$mainImage = '#' . $fullPath;
$imgData = array(
'picture' => $mainImage
);
try {
$data = $this->facebook->api('/'.$eventId, 'post', $imgData);
return $data;
} catch (FacebookApiException $e) {
error_log('Failed to attach picture to event. Exception: ' . $e->getMessage());
}
return null;
}
Maybe it will work for you, for me it stopped to work since Facebook issue was discovered.

Categories