使用PHP表单更新Twitter状态

时间:2010-10-20 23:37:45

标签: php twitter curl

我有以下代码,但它不会更新我的状态(此代码段的用户名和密码已被删除)并且它会引发错误eeek。任何想法为什么它不起作用?

编辑:

显然Twitter现在需要OAuth。我一直在这里关注本教程:http://ditio.net/2010/06/07/twitter-php-oauth-update-status/

我已经注册了我的应用程序并复制了库和代币。

但我不确定如何使用我当前的代码,即。什么需要改变/添加/删除。

我的更新代码:

<?php

    function postToTwitter($username,$password,$message)
    {

        require_once 'TwitterOAuth.php';

        define("CONSUMER_KEY", "???");
        define("CONSUMER_SECRET", "???");
        define("OAUTH_TOKEN", "???");
        define("OAUTH_SECRET", "???");

        $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET);
        $content = $connection->get('account/verify_credentials');

        $connection->post('statuses/update', array('status' => date(DATE_RFC822)));

        // GET the API url via web authentication
        // add 'status' param to send a message to Twitter

        $host = "http://api.twitter.com/1/statuses/update.xml?status=".urlencode(stripslashes(urldecode($message)));

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $host);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));

        curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_POST, 1);

        // Go for it!!!
        $result = curl_exec($ch);
        // Look at the returned header
        $resultArray = curl_getinfo($ch);

        // close curl
        curl_close($ch);

        //echo "http code: ".$resultArray['http_code']."<br />";

        if($resultArray['http_code'] == "200"){
            echo "<br />OK! posted to http://twitter.com/".$username."/<br />";
        } else {
            echo "eek! yegads! error posting to Twitter";
        }

        // debug the result
        // echo "<pre>";
        // print_r($resultArray);
        // echo "</pre><hr>";

        // $sResult = htmlentities($result);
        // $sResult = str_replace("&gt;&lt;","&gt;<br />&lt;",$sResult);

        // echo "<pre>";
        // print $sResult;
        // echo "</pre>";

    }

    if(isset($_POST['submit']))
    {
        $textmessage = $_POST['message'];
        postToTwitter("whiningwall","u0558234",$textmessage);
    }

?><!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <meta name="viewport" content="width=1024">
        <title></title>
        <link rel="stylesheet" type="text/css" href="master.css" media="screen" />
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> 
    </head>

    <body>

        <form id="post" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

            <fieldset>

                <label for="textArea">What's on your mind?</label>
                <textarea id="textArea" name="message" maxlength="140"></textarea>

                <div class="submit">
                    <span class="charsRemaining"></span>
                    <input type="submit" name="submit" value="Share" />
                </div>

            </fieldset>

        </form>

        <script type="text/javascript" src="jquery.maxlength.js"></script>
        <script type="text/javascript">
            $('#textArea').focus();
        </script>

    </body>
</html>

1 个答案:

答案 0 :(得分:2)

我相信CURLOPT_USERPWD用于Apache的基本身份验证,Twitter不使用。我对Twitter API了解不多,但我认为你最好用OAuth告诉Twitter你是谁。

编辑添加:http://code.google.com/p/php-twitter/的GPL推特库可能会尝试查看(甚至使用)。