Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the prerequisites for Facebook stream.publish?

Tags:

php

facebook

api

I'm wondering if someone would help me troubleshoot my test for stream.publish. I thought I had all the right pieces. Here's the code:

<?php
require_once 'facebook.php';
$appapikey = 'xxxxxxx';
$appsecret = 'xxxxxxx';
$facebook = new Facebook($appapikey, $appsecret);
$user_id = $facebook->require_login();


$message = "Will this status show up and allow me to dominate the world?!";
$uid = $user_id;
echo $uid;
$facebook->api_client->stream_publish($message,$uid);

What I'm expecting is my status to change to $message's content. What happens instead is that my UID is echo'd, and then it throws a 500 error. I've allowed publish_stream as well as offline_access (verified in my app settings, via my profile), the the API key hooks this small bit of code to my app. What other pieces do I need to make this simple example work? I'm finding the FB documentation a little hard to put together.

-- The include is the official PHP Facebook library

like image 545
Alex Mcp Avatar asked Mar 01 '23 12:03

Alex Mcp


2 Answers

stream_publish() takes more than two arguments:

stream_publish($message, $attachment = null, 
               $action_links = null, $target_id = null, 
               $uid = null)

Where $target_id is the user or page you're publishing to and $uid is the user or page who is doing the publishing - and which defaults to your session id. To be completely explicit about this, I think you need to try

<?php
require_once 'facebook.php';
$appapikey = 'xxxxxxx';
$appsecret = 'xxxxxxx';
$facebook = new Facebook($appapikey, $appsecret);
$user_id = $facebook->require_login();

$message = "Will this status show up and allow me to dominate the world?!";

echo $user_id;
$facebook->api_client->stream_publish($message,null,null,$user_id,$user_id);

An alternate form might be:

$app_id = 'xxxxxxx'; 
$facebook->api_client->stream_publish($message,null,null,$user_id,$app_id);
like image 140
Mike Heinz Avatar answered Mar 03 '23 02:03

Mike Heinz


This one works in 2011! I had the same problem. Most of the tuts seem to be out of date thanks to Facebook changes. I eventually found a way that worked and did a quick blog article about it here:

http://facebookanswers.co.uk/?p=214

There's also a screen shot to show you what the result is. Make sure you also see the blog post about authentication though.

like image 44
Relaxing In Cyprus Avatar answered Mar 03 '23 03:03

Relaxing In Cyprus