twitter api更新状态更新但没有来自Twitter的回复

时间:2012-12-28 17:10:20

标签: php codeigniter twitter

对于twitter API有点问题。当我向https://api.twitter.com/1/statuses/update.json发送内容时,会发送推文(状态更新),但是,我没有收到来自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

1 个答案:

答案 0 :(得分:0)

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

您可以更轻松地使用twitter-async(https://github.com/jmathai/twitter-async)

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

易于使用&记录完备。