0

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

4

1 回答 1

0

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

你会更容易使用 twitter-async (https://github.com/jmathai/twitter-async)

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

它易于使用且有据可查。

于 2012-12-29T15:49:04.183 回答