我的网站上有一个用户可以编辑的文本区域,最多 140 个字符。我有两个提交按钮 - 一个用于发布到 Facebook,一个用于发布到 Twitter。我能够让 Facebook 按钮正常工作,但我被 Twitter 按钮卡住了一半。
在用户点击按钮发推的那一刻,我只能让它发推到我自己的账户。我希望这个转到用户的帐户。我意识到这是因为我在我的 PHP 中定义了我的 oAuthToken / Secret,但我不确定如何请求用户登录 / 授权我的应用程序,从而创建和使用他们的 oAuth 信息。我似乎无法在这里或其他任何地方找到适合我的答案,所以一些帮助或正确方向的点会很棒!
到目前为止,这是我的代码:
<?php if($_SERVER['REQUEST_METHOD'] == "POST") { ?>
<?php $statusupdate = $_POST['text']; ?>
<?php $socialsubmit = $_POST['socialsubmit']; ?>
<?php if($socialsubmit == "share") { ?>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '182922938522212', // App ID
channelUrl : 'channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.ui({
method: 'feed',
name: 'Minty Delivers',
caption: 'Same Day Grocery Delivery in Halifax',
description: '<?php echo $statusupdate; ?>',
link: 'http://www.mintydelivers.com',
picture: 'http://www.mintydelivers.com/connect-share.png'
});
};
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
</script>
<?php echo $socialsubmit;?>
<?php } else if($socialsubmit == "tweet") { ?>
<?php
$consumerKey = 'xxx';
$consumerSecret = 'xxx';
$oAuthToken = 'xxx';
$oAuthSecret = 'xxx';
require_once('tw/twitteroauth.php');
// create a new instance
$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $oAuthToken, $oAuthSecret);
//send a tweet
$tweet->post('statuses/update', array('status' => $statusupdate));
?>
<div class="alert alert-success">
Your tweet was successfully posted. Thanks for sharing!
</div>
<?php } ?>
<?php } ?>
<form id="status-update" action=""; method="post">
<textarea name="text" onKeyPress="return charLimit(this)" onKeyUp="return characterCount(this)">I found this GREAT company called Minty that delivers groceries, wine, flowers and gifts anywhere in Halifax! www.mintydelivers.com</textarea>
<p><span id="charCount">9</span> characters left</p>
<button id="twitter" class="tweetpopup" name="socialsubmit" value="tweet" type="submit">Tweet This</button>
<button id="facebook" name="socialsubmit" value="share" type="submit">Share This</button>
</form><!-- end form -->