如何使用onclick属性在墙上发布?

时间:2013-05-09 20:35:08

标签: php html facebook facebook-php-sdk facebook-apps

我尝试了几种不同的方法来让我的数据仅在用户点击时发布,但无济于事。

希望有人可以提供帮助:

<link href="facebook.css" rel="stylesheet" type="text/css">
<div class="fbbody">

<?php

require './facebook.php';

$facebook = new Facebook(array(
  'appId'  => ' *******',
  'secret' => '******',
  'cookie' => true, 
));

$uid = $facebook->getUser();

$status = $_POST['status'];


if($status == "")

$msg = "Please enter a status.";

else {

$msg = "Thanks.";

}


?>

<script>
alert('<? echo $msg; ?>');
</script>


<div align="center">
<form method="GET" action="translate.php">
<textarea name="status2" cols="50" rows="5"/>
<?php echo str_ireplace(array ('old','awkward','all','again','behind','along','alright','hello','among','children','yes','child','kids','food','barnard castle','beer','book','blow','beautiful','bird','burst','brown','burn','boots'), 
array ('auld', 'aakwad', 'aall','agyen','ahint','alang','alreet','alreet','amang','bairns','aye','bairn','bairns','bait','barney','beor','beuk','blaa','bonny','bord','borst','broon','bourn','byeuts'),$status); ?> 

</textarea><br>


<?php


$args = array(
'message'   => $_GET['status2'],
'link'      => 'http://apps.facebook.com/geordie-status/',
'caption'   => 'Translate from English to Geordie'
);
$post_id = $facebook->api("/$uid/feed", "post", $args);

?>

<input type="submit" value="post to wall"/>

</form>

</div>


</div>

上面的代码是我的translate.php。

我如何使用'onclick'属性在单击时运行以下代码,并且仅在单击按钮时才运行,而不是之前。

<?php


$args = array(
'message'   => $_GET['status2'],
'link'      => 'http://apps.facebook.com/geordie-status/',
'caption'   => 'Translate from English to Geordie'
);
$post_id = $facebook->api("/$uid/feed", "post", $args);

?>

1 个答案:

答案 0 :(得分:0)

要发布状态消息,您必须要求用户提供权限,请参阅:https://developers.facebook.com/docs/reference/login/extended-permissions/您需要在这里发布publish_actions。

因为您希望发布点击事件,您将在显示表单之前询问您的许可。

您可以使用jquery来处理您的点击事件(在下面的代码中提交)。 jquery用于将消息发送到第二页(ajax请求)。第二页(facebookpost.php)将在墙上发布消息。

<强> facebook.php

<?
error_reporting(E_ALL);
ini_set('display_errors','on');
require 'facebook-php-sdk-master/src/facebook.php';

$facebook = new Facebook(array(
  'appId'  => '***************',
  'secret' => '*******************************',
));

$applicationurl = 'http://testdrive.nl/facebook.php';

// Get User ID
$user = $facebook->getUser();

if(empty($user))
{
    $params = array(
  'scope' => 'publish_actions',
  'redirect_uri' => $applicationurl
   );   

$loginUrl = $facebook->getLoginUrl($params);
header('Location: ' . $loginUrl ."\r\n");
exit;
}   
?>
<!DOCTYPE html>
<html>
<head>
<title>Facebook testpage</title>
</head>
<body>

<div class="container">
    <div id="result"></div>
<form id="poster">  
<textarea id="status2"></textarea>  
<input type="submit" value="post to wall"/>
</form>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>    
<script>
$('#poster').submit(function() {
$.get("facebookpost.php", { status2: $('#status2').val() }, function(data) {
$('#result').html(data);
});
return false;
});
</script>
</body>
</html>

<强> facebookpost.php

<?
error_reporting(E_ALL);
ini_set('display_errors','on');
require 'facebook-php-sdk-master/src/facebook.php';

$facebook = new Facebook(array(
      'appId'  => '***************',
      'secret' => '*******************************',
));

//$applicationurl = 'http://testdrive.nl/facebook.php';

// Get User ID
$user = $facebook->getUser();

if(empty($user))
{
    echo 'invalid user';
}   

$args = array(
'message'   =>  $_GET['status2'],
'link'      => 'http://apps.facebook.com/geordie-status/',
'caption'   => 'Translate from English to Geordie'
);
$facebook->api("/$user/feed", "post", $args);
echo 'posted to your wall';