views:

91

answers:

3

Hello,

I am migrating my Facebook canvas application to using the new PHP SDK. However, I am having a problem getting the JavaScript SDK to work too.

I'm wanting to take advantage of methods such as stream.publish in the JavaScript SDK. Unfortunately I've not been able to get anything to work thus far. I have the following in the header of my application's index.php file:

<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_GB/all.js"&gt;&lt;/script&gt;
<script>
    FB.init({appId: *);
    FB.ui({
        method: 'stream.publish',
        message: 'Check out this great app!'
    });
</script>

My application ID has been removed for obvious reasons.

Where I'd expect the above to give the user a prompt to publish a message to their stream, it's not. Can I even use the new JavaScript SDK in Facebook canvas applications? Or is it reserved for iFrame applications and utilizing Facebook Connect on websites only?

A: 

Sorry, you can't use JavaScript SDK or even pure JavaScript in FBML canvas apps. Facebook created their own replacement of JavaScript called FBJS with limited functionality (for security reasons). So in canvas app you can use only PHP SDK, FBML and FBJS. In order to publish on the wall you need to use PHP SDK probably.

serg
A: 

Make sure you're building a IFrame Canvas application, and not a FBML application. The JavaScript SDK only works with IFrames. Here's an example of stream.publish on Canvas: http://apps.facebook.com/fbrelll/fb.ui/stream.publish

daaku
A: 

I've solved it using the following:

<?php
$attachment = array(
    'name' => 'Attachment name here',
    'href' => 'Attachment URL here',
    'media' => array(array(
        'type' => 'image',
        'src'  => 'Image URL here',
        'href' => 'Link URL here',
    )),
    'caption' => 'Caption here',
);
$action_links = array(array(
    'text' => 'Action link text',
    'href' => 'Action link href',
));
?>
<script type="text/javascript">
<!--
var message = "Your status update here...";
var attachment = <?php echo json_encode($attachment); ?>;
var action_links = <?php echo json_encode($action_links); ?>;
Facebook.streamPublish(message, attachment, action_links);
//-->
</script>

Thanks to all that answered.

Martin Bean
I'm trying to figure out how to publish to my wall of my application from my application (i.e. an event from the web site would publish a wall post). Do you know how to do that? Also you reference Facebook.streamPublish in your Javascript code. How are you getting access to Facebook through that?
Jeff V
I'm not sure how I have access to the `Facebook` object in my script above, but it seems to work for me. As for posting to your application wall, do you mean your application's profile page?
Martin Bean