Twitter API更新状态正在更新,但Twitter没有响应


twitter api update status updating but no response from twitter

twitter API有点问题。当我发送一些东西到https://api.twitter.com/1/statuses/update.json时,tweet(状态更新)确实被发送,但是,我没有得到twitter的响应。当我向任何其他api url发送请求时,它们按预期工作并返回响应。请参阅下面的代码…

function postStatus($oauthToken,$status) {
    //Create sig base string
    $tokenddata = array('oauth_token'=>$oauthToken['oauth_token'],'oauth_token_secret'=>$oauthToken['oauth_token_secret']);
    $status = rawurlencode($status);
    $baseurl = $this->baseurl . "statuses/update.json";
    $url = "{$baseurl}?status={$status}";
    $authHeader = get_auth_header($url, $this->_consumer['key'], $this->_consumer['secret'],
                            $tokenddata, 'POST', $this->_consumer['algorithm']);
    $postfields = "status={$status}";
    $response = $this->_connect($url,$authHeader,$postfields,'POST');
    return json_decode($response);
}
private function _connect($url, $auth, $postfields=null, $method='GET') {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ;
    curl_setopt($ch, CURLOPT_SSLVERSION,3);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array($auth));
    if ($method == 'POST') {
        curl_setopt($ch, CURLOPT_POST, TRUE);
        if (!empty($postfields)) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
        }
    }
    $curl_info = curl_getinfo($ch);
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}

正如我之前所说,我使用的其他请求是'GET'请求,并使用下面的代码…

function getFromTwitter($url, $oauthToken, $params=null) {
    $tokenddata = array('oauth_token'=>$oauthToken['oauth_token'],'oauth_token_secret'=>$oauthToken['oauth_token_secret']);
    $baseurl = $this->baseurl . $url;
    if(!empty($params)) {
        $fullurl = $baseurl . "?" . build_http_query($params);
        $postfields = build_http_query($params);
        $authHeader = get_auth_header($fullurl, $this->_consumer['key'], $this->_consumer['secret'],
                            $tokenddata, 'GET', $this->_consumer['algorithm']);
    } else {
        $authHeader = get_auth_header($baseurl, $this->_consumer['key'], $this->_consumer['secret'],
                            $tokenddata, 'GET', $this->_consumer['algorithm']);
    }
    if(!empty($postfields)) {
        $response = $this->_connect($fullurl,$authHeader);
    } else {
        $response = $this->_connect($baseurl,$authHeader);
    }
    return json_decode($response);
}

感谢所有的帮助!sm

自己实现社交网络代码可能会很痛苦(在我看来)

使用twitter-async (https://github.com/jmathai/twitter-async)会更方便。

我之前已经将它添加到我的CI中作为辅助函数,然后原样使用它。

使用&良好的文档记录。