2

我的网站上有一个用户可以编辑的文本区域,最多 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 -->
4

1 回答 1

1

用户需要授权您的应用程序代表他们发推文。这包括将他们重定向到 Twitter 并让他们授予对您的应用程序的访问权限。关于这个主题的 twitter 文档在这里:

https://dev.twitter.com/docs/auth/obtaining-access-tokens

在 PHP 中实现此行为的一种简单方法是通过 Temboo 的 OAuth 包装器。Temboo 将 OAuth 过程分为两个调用,Initialize第一个调用Finalize返回您需要将用户定向到的 Twitter 授权 URL。该过程的第二步返回您需要代表您的用户发布推文的访问令牌。您可以从 Temboo 的网站测试 Twitter OAuth,然后通过 Temboo PHP SDK 在您的代码中使用它。

https://www.temboo.com/library/Library/Twitter/OAuth/

(全面披露:我在 Temboo 工作)

于 2013-03-25T13:10:26.467 回答