修改此代码以检索提及或Hashtags

时间:2013-12-12 02:28:22

标签: php twitter

昨天,我发布了关于Search for Mentions of a Topic的Twitter API。我询问使用什么URL,因为我认为我做错了。

事实证明,我最初猜测使用哪个URL是正确的(因为我应该使用Twitter Dev Console来获取URL。问题似乎是在实际使用URL中我找到的代码。我对Twitter API的理解非常有限,但现在做简单的事情似乎很难......

我正在尝试使用this code (which I found in a blog post)来访问推文。当我加载一个只有这个代码的PHP页面时(当然还有我的信息),我从我的个人帐户中看到推文没有问题(我当然在我的帐户上设置了一个应用程序)。但是当我尝试更改代码本身中的两个URL时,我得到{"errors":[{"message":"Could not authenticate you","code":32}]}那么我还需要修改代码呢?

注意:HERE是我找到代码的原始博文。

编辑:这是我正在使用的代码(好吧,减去我的信息):

<?php 

//After you create the app at http://dev.twitter.com, you'll need to get the following four pieces of data from the details tab in your app's page.
$consumer_key = '';
$consumer_secret = '';
$access_token = '';
$access_token_secret = '';

$oauth_hash = 'oauth_consumer_key='.$consumer_key.
  '&oauth_nonce='.time().
    '&oauth_signature_method=HMAC-SHA1&oauth_timestamp='.
    time().'&oauth_token='.$access_token.'&oauth_version=1.0';

// $base = 'GET&'.rawurlencode('http://api.twitter.com/1.1/search/tweets.json?q=apple&result_type=recent&count=10‌').
//  '&'.rawurlencode($oauth_hash);
    $base = 'GET&'.rawurlencode('https://api.twitter.com/1.1/statuses/user_timeline.json').
    '&'.rawurlencode($oauth_hash);


$key = rawurlencode($consumer_secret).'&'. rawurlencode($access_token_secret);

$signature = base64_encode(hash_hmac('sha1', $base, $key, true));
$signature = rawurlencode($signature);

$oauth_header = 'oauth_consumer_key="'.$consumer_key.'",
    oauth_nonce="' . time() . '",oauth_signature="' . $signature . '",
    oauth_signature_method="HMAC-SHA1",
    oauth_timestamp="' . time() . '",
    oauth_token='.$access_token.',
    oauth_version="1.0", ';

$curl_header = array("Authorization: Oauth {$oauth_header}", 'Expect:');


$curl_request = curl_init();
    curl_setopt($curl_request, CURLOPT_HTTPHEADER, $curl_header);
    curl_setopt($curl_request, CURLOPT_HEADER, false);
    // curl_setopt($curl_request, CURLOPT_URL, 'http://api.twitter.com/1.1/search/tweets.json?q=apple&result_type=recent&count=10‌');
    curl_setopt($curl_request, CURLOPT_URL, 'https://api.twitter.com/1.1/statuses/user_timeline.json');
    curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, false);
$json = curl_exec($curl_request);
    curl_close($curl_request);
echo $json;

1 个答案:

答案 0 :(得分:0)

就个人而言,我会尝试一下Twitter推荐的PHP库(这个库看起来轻巧而且很好:https://github.com/J7mbo/twitter-api-php)。

在其他新闻中,您正在使用的跟进帖子更正了此问题:http://blog.jacobemerick.com/web-development/passing-extra-parameters-to-twitter-via-oauth/

哈希变更:

$oauth_hash = '';
$oauth_hash .= 'count=TOTAL_COUNT_YOU_WANT&';
$oauth_hash .= 'oauth_consumer_key=YOUR_CONSUMER_KEY&';
$oauth_hash .= 'oauth_nonce=' . time() . '&';
$oauth_hash .= 'oauth_signature_method=HMAC-SHA1&';
$oauth_hash .= 'oauth_timestamp=' . time() . '&';
$oauth_hash .= 'oauth_token=YOUR_ACCESS_TOKEN&';
$oauth_hash .= 'oauth_version=1.0&';
$oauth_hash .= 'screen_name=SCREEN_NAME_HERE';

标题更改:

$oauth_header = '';
$oauth_header .= 'count="TOTAL_COUNT_YOU_WANT", ';
$oauth_header .= 'oauth_consumer_key="YOUR_CONSUMER_KEY", ';
$oauth_header .= 'oauth_nonce="' . time() . '", ';
$oauth_header .= 'oauth_signature="' . $signature . '", ';
$oauth_header .= 'oauth_signature_method="HMAC-SHA1", ';
$oauth_header .= 'oauth_timestamp="' . time() . '", ';
$oauth_header .= 'oauth_token="YOUR_ACCESS_TOKEN", ';
$oauth_header .= 'oauth_version="1.0", ';
$oauth_header .= 'screen_name="SCREEN_NAME_HERE"';

卷曲变化:

curl_setopt($curl_request, CURLOPT_URL, 'https://api.twitter.com/1.1/statuses/user_timeline.json?count=TOTAL_COUNT_YOU_WANT&screen_name=SCREEN_NAME_HERE');