How to implement "Suggest MY-PAGE to friends" using PHP-SDK or using Javascript SDK?
问题:
回答1:
First you need the Facebook SDK bundle that contains base_facebook.php, facebook.php and fb_ca_chain_bundle.crt . You will also need fbmain.php and config.php .
Next you should have a file (e.g postToWall.php) that includes fbmain.php
<?php
include_once "fbmain.php";
?>
An example of postToWall.php file.
<html>
<body id="my_body">
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId: '<?php echo $facebook->getAppID() ?>',
cookie: true,
xfbml: true,
oauth: true
});
FB.Canvas.setAutoGrow();
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>
<?php
if ($me)
{
$params = array('message' => "message here",
'picture' => "picture hyperlink here",
'name' => "name here",
'link' => "facebook page hyperlink here",
'description' => " description here"
);
$status = $facebook->api('/me/feed', 'POST', $params);
if (isset($status['id']))
{
//do something
}
}
?>
</body>
</html>
Credits to my tutor, Mr Zen Leow
回答2:
The earlier solution I have posted is using the PHP SDK method.
Using JavaScript SDK, you can have a HTML button using onclick attribute to call a function.
<input type="button" value="Share" onclick="share();"/>
Inside share function, the method property is compulsory and the other properties (link, picture, name, caption, description) are optional. The value "feed" in method property refers to feed dialog which you need, there are other values for method property such as "apprequests" (Request Dialog) and "send" (Send Dialog). For more information, check out http://developers.facebook.com/docs/reference/dialogs/
<script>
function share()
{
var obj = {
method: "feed",
link: "Facebook page hyperlink",
picture: "Picture hyperlink",
name: "Title",
caption: "A short caption right below the title",
description: "Description"
};
function callback(response) {
document.getElementById('msg').innerHTML = "Post ID: " + response['post_id'];
}
FB.ui(obj, callback);
}
</script>
The difference between PHP SDK and JavaScript SDK
For PHP SDK, the default message specified in postToWall.php will be posted straight to the user's wall upon clicking the share button. You have to redirect user to postToWall.php first and then redirects him/her back to your application page.
For JavaScript SDK, a window will pop up upon clicking the share button and user will be able to input their own message before he/she sends it. No additional redirection will be needed after sending as user is still on the same page.
P.S.: I'm still learning Facebook and PHP at the moment, so do correct me if I am making any mistake haha. Thanks =)