dongshan3806 2010-10-20 23:37 采纳率: 0%
浏览 43

使用PHP表单更新Twitter状态

I have the following code, but it doesn't update my status (the username and password has been removed for this snippet) and it throws the error eeek. Any ideas why it's not working?

EDIT:

Apparently Twitter NEEDS OAuth instead now. I have been following this tutorial here: http://ditio.net/2010/06/07/twitter-php-oauth-update-status/

I have registered my App and copied both the Library and Tokens over.

BUT I'm not sure how to get this working with my current code, ie. what needs changing/adding/removing.

My Updated code:

<?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条回答 默认 最新

  • duanbai1974 2010-10-20 23:44
    关注

    I believe that CURLOPT_USERPWD is for Apache's basic authentication, which Twitter does not use. I don't know much about the Twitter API but I think you'll be better off using OAuth to tell Twitter who you are.

    Edit to add: There's GPL twitter library at http://code.google.com/p/php-twitter/ that you might try looking at (or even using).

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。