2

我正在使用我在互联网上找到的修改后的 php 脚本作为 javascript 和 twitter api 1.1 之间的代理(我需要这样做,因为 javascript 不能执行 oauth 操作,而 api 1.1 正是需要:身份验证)。

该脚本工作正常 - 直到我搜索主题标签,然后 oauth 失败。

这是我在搜索 @z25org 时从 twitter 获得的 curl_info 示例

url: http://api.twitter.com/1.1/search/tweets.json?q=%40z25org
content_type: application/json;charset=utf-8
http_code: 200

如您所见,这很有效(http_code:200)。但是当我搜索主题标签时:

url: http://api.twitter.com/1.1/search/tweets.json?q=%23z25org
content_type: application/json; charset=utf-8
http_code: 401

我得到 http_code 401:未经授权的访问。json:

{"errors":[{"message":"Could not authenticate you","code":32}]}

这是我的 php 代码:(嗯,它的最大部分)

<?php
// Some characters that need to be replaced
$specialCharacters = array(
    "@"=>"%40",
    "#"=>"%23",
    " "=>"%20",
    ""=>""
);

/*
* Ok, no more config should really be needed. Yay!
*/

// We'll get the URL from $_GET[]. Make sure the url is url encoded, for example encodeURIComponent('statuses/user_timeline.json?screen_name=MikeRogers0&count=10&include_rts=false&exclude_replies=true')
if(!isset($_GET['url'])){
    die('No URL set');
}

$url = $_GET['url'];

// Figure out the URL parmaters
$url_parts = parse_url($url);
parse_str($url_parts['query'], $url_arguments);

$full_url = $config['base_url'].$url; // Url with the query on it.
$base_url = $config['base_url'].$url_parts['path']; // Url without the query.

if (!dbglog(" > ORIGINAL: ".$full_url)) { die("Huh?"); }

// Replace characters
foreach($specialCharacters as $lookup => $replace) {
    $full_url = str_replace($lookup,$replace,$full_url);
}

if (!dbglog(" > REPLACED: ".$full_url)) { die("Huh?"); }

/**
* Code below from http://stackoverflow.com/questions/12916539/simplest-php-example-retrieving-user-timeline-with-twitter-api-version-1-1 by Rivers 
* with a few modfications by Mike Rogers to support variables in the URL nicely
*/

function buildBaseString($baseURI, $method, $params) {
    $r = array();
    ksort($params);
    foreach($params as $key=>$value){
    $r[] = "$key=" . rawurlencode($value);
    }
    return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
}

function buildAuthorizationHeader($oauth) {
    $r = 'Authorization: OAuth ';
    $values = array();
    foreach($oauth as $key=>$value)
    $values[] = "$key=\"" . rawurlencode($value) . "\"";
    $r .= implode(', ', $values);
    return $r;
}

// Set up the oauth Authorization array
$oauth = array(
    'oauth_consumer_key' => $config['consumer_key'],
    'oauth_nonce' => time(),
    'oauth_signature_method' => 'HMAC-SHA1',
    'oauth_token' => $config['oauth_access_token'],
    'oauth_timestamp' => time(),
    'oauth_version' => '1.0'
);

$base_info = buildBaseString($base_url, 'GET', array_merge($oauth, $url_arguments));
$composite_key = rawurlencode($config['consumer_secret']) . '&' . rawurlencode($config['oauth_access_token_secret']);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
$oauth['oauth_signature'] = $oauth_signature;

// Make Requests
$header = array(
    buildAuthorizationHeader($oauth), 
    'Expect:'
);
$options = array(
    CURLOPT_HTTPHEADER => $header,
    //CURLOPT_POSTFIELDS => $postfields,
    CURLOPT_HEADER => false,
    CURLOPT_URL => $full_url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SSL_VERIFYPEER => false
);

try {
    $feed = curl_init();
    curl_setopt_array($feed, $options);
    $result = curl_exec($feed);
    $info = curl_getinfo($feed);
    curl_close($feed);
} catch (Exception $e) {
    die("Error: ".$e);
}

// Send suitable headers to the end user.
if(isset($info['content_type']) && isset($info['size_download'])){
    header('Content-Type: '.$info['content_type']);
    header('Content-Length: '.$info['size_download']);

}

echo($result);
?>
4

1 回答 1

1

我在构建这个库时遇到了同样的问题。

此提交特别可能会产生更多信息。

您遇到的问题基本上是您的 URL 编码不正确 - 甚至可能是两次。如果您从上面的源代码查看代码(它非常相似,似乎已在此处进行了修改),那么它将完全按照您的意愿工作。

TLDR:改用上面的单个文件,或者只是复制/粘贴代码。这几乎是你想要的。

正如 OP 发现的那样:他还尝试用%2523(where %25= %) 替换它。仍然建议查看上述库。

于 2013-06-28T13:40:33.863 回答