dongyoulou4829 2013-12-12 02:28
浏览 36

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

Yesterday, I posted about Search for Mentions of a Topic with the Twitter API. I asked about what URL to use, since I thought I was doing it wrong.

As it turns out, my initial guess on which URL to use was correct (as it should have been, since I used Twitter Dev Console to get the URL. The problem seems to be in actually using the URL in the code I found. I have very limited understanding of the Twitter API, but it seems to be very difficult to do simple things now...

I'm trying to use this code (which I found in a blog post) to access tweets. When I load a PHP page with just this code (with my information in it, of course), I have no problem seeing tweets from my personal account (I DO have an application set up on my account, of course). But when I try to change the two URLs in the code itself, I get {"errors":[{"message":"Could not authenticate you","code":32}]}. So how else do I need to modify the code?

Note: HERE is the original blog post I found the code for.

EDIT: Here is the code I'm using exactly (well, minus my info):

<?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条回答 默认 最新

  • dsm42026 2013-12-12 05:32
    关注

    Personally I would try one of Twitter's recommended PHP libraries (this one looks lightweight and good: https://github.com/J7mbo/twitter-api-php).

    In other news, there was a follow up post to the one you are working with that corrected this issue: http://blog.jacobemerick.com/web-development/passing-extra-parameters-to-twitter-via-oauth/

    Hash Change:

    $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';
    

    Header Change:

    $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 change:

    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');

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器