在ajax调用中更新twitter状态,使用jmathai twitter async';的图书馆


Updating twitter status in an ajax call, using jmathai-twitter-async's library

我有一个带有文本框和按钮的页面,我想把文本框的内容发布到twitter上。

我在dev.twitter上创建了一个应用程序,我首先检查用户是否经过身份验证,然后将以下内容添加到按钮的点击事件中:

//sending message
$.post('postStatus.php',{
    status:message
    },function(response){
    $("#debug").html(response);
});

文件postStatus.php为(初稿):

<?php
    require_once 'EpiCurl.php';
    require_once 'EpiOAuth.php';
    require_once 'EpiTwitter.php';
    require_once 'secret.php';
    //is connected ?
    /*
    $twitterObj = new EpiTwitter($consumerKey, $consumerSecret);
    $oauth_token = @$_GET['oauth_token'];
    if($oauth_token == '')
        die();
    */
    //validate message
    $message=@$_POST['status'];
    $message = trim($message);
    if ($message === '')
        die();
    //send
    $twitterObj->setToken($_COOKIE['oauth_token'],$_COOKIE['oauth_token_secret']);
    $status=$twitterObj->post_statusesUpdate(array('status' => $message));
    $status->response;
    echo 'Your status has been updated';

我在验证$message时遗漏了什么吗?如果有什么东西,我应该用try/catch块包起来?最后,我应该让postStatus.php返回500错误,还是die()可以?

在您的PHP中,$consumerKey$consumerSecret没有定义,而且永远不会设置$_GET['oauth_token'],因为js中的url是静态的。

此外,您可能希望在attpemt发布之前逃离状态。