0

我的一门课程中有以下代码:

public function build_httpheader ($options = NULL)
{
    if (!$this->oauth_token && !$this->oauth_token_secret && !$this->http_method) {
        throw new Exception('The oauth_token, oauth_token_secret and the http method must be set');
    }
    $url = $this->base_uri;
    $url_curl = $this->base_uri;
    if ($options) {
        $this->options = $options;
        $url .= '?';
        $url_curl .= '?';
        $count = count($options);
        foreach($options as $key => $value) {
            $count = $count--;
            if ($count) {
                $url .= '&'.$key.'='.$value;
                $url_curl .= '&'.$key.'='.rawurlencode($value);
            } else {
                $url .= $key.'='.$value;
                $url_curl .= $key.'='.rawurlencode($value);
            }
        }
    }
    $this->url = $url_curl;
    /**
     * @internal Create a 32 chars unique string to
     * use as the nonce value
     */
    list($usec, $sec) = explode(" ", microtime());
    $usec = str_replace('0.', '', $usec);
    $nonce_str = utf8_encode($sec.$usec.'ABCD'.$sec);
    $oauth_nonce =  md5($nonce_str);
    /**
     * @internal Create the initial oAuth array
     */
    $oauth = array (
        'oauth_consumer_key' => $this->consumer_key,
        'oauth_nonce' => $this->$oauth_nonce,
        'oauth_signature_method' => 'HMAC-SHA1',
        'oauth_token' => $this->oauth_token,
        'oauth_timestamp' => time(),
        'oauth_version' => '1.0'
    );
    /**
     * @internal generate basic info
     */
    $t_oauth = array();
    ksort($oauth);
    foreach($oauth as $key=>$value){
        $t_oauth[] = "$key=" . rawurlencode($value);
    }
    $base_info = $this->http_method."&" . rawurlencode($url) . '&' . rawurlencode(implode('&', $t_oauth));
    $composite_key = rawurlencode($this->consumer_secret) . '&' . rawurlencode($this->oauth_token_secret);
    $oauth['oauth_signature'] = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));

    $oauth_string = 'Authorization: OAuth ';
    $values = array();
    foreach($oauth as $key=>$value) {
        $values[] = "$key=\"" . rawurlencode($value) . "\"";
    }
    $oauth_string .= implode(', ', $values);
    $this->http_headers = array ($oauth_string, 'Expect:');
}

public function tw_curl_api_call ()
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, $this->http_headers);
    if ($this->http_method == 'POST') {
        curl_setopt($ch, CURLOPT_POST, true);
    }
    if ( ($this->http_method != 'POST') && ($this->http_method != 'GET') ) {
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->http_method);
    }
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_URL, $this->url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);

    $result = json_decode(curl_exec($ch), TRUE);
    $info = curl_getinfo($ch);
    curl_close($ch);
    return array ('result' => $result, 'info' => $info);
}

另一个脚本使用这个类,如下所示:

$options = array ('resources' => 'help,users,search,statuses');

$tw_wrapper->build_httpheader ($options);

$results = $tw_wrapper->tw_curl_api_call ();

但是,我收到错误 215(错误的身份验证数据)。任何想法(我知道有现有的 PHP oAuth 类和 twitter 包装器,但似乎它们中的任何一个都没有完全迁移到 1.1 API)。

4

1 回答 1

0

你看过Codebird 吗?它支持 1.1 并且可以很好地满足我的需要。当然是 YMWV。

一个简单的例子:

Codebird::setConsumerKey('KEY', 'SECRET');

$cb = Codebird::getInstance();
$cb->setToken('AUTH_KEY', 'AUTH_SECRET');

$result = $cb->statuses_userTimeline(array(
    'include_rts' => true,
    'screen_name' => 'hansnilsson',
));
于 2013-02-19T12:13:05.007 回答