Твиттер API поиска в Твиттере не работает с хеш-тегом

Привет прямо сейчас я пытаюсь сделать поиск по ключевым словам с помощью хэштега, используя Twitter API. Это URL, который я использую.

https://api.twitter.com/1.1/search/tweets.json?q=%23bookmyshow

Но я не получаю никакого результата. Со вчерашнего дня я пытался разобраться в этом вопросе. Любой может помочь мне решить эту проблему. Это код, который я использую для поиска

<?php
ini_set('display_errors', 1);
require_once('TwitterAPIExchange.php');


    $oauth_access_token = '2329813950-XGm12JrlbxOIHF6mmDPhF8l2ddDHa2PEKPdHYHp';
    $oauth_access_token_secret = '1g88J15Qxl24SOn6arfXgAqGH0N1VthxvDIyrK2dZBfu1';
    $consumer_key = 'mU6nzH298ZoZCdYaqbyzA';
    $consumer_secret = 'gIDEYBiruLf29VEq7Zx75U7bFJrkia9HV8SSw0qjlI';

    $token = '2329813950-XGm12JrlbxOIHF6mmDPhF8l2ddDHa2PEKPdHYHp';
    $token_secret = '1g88J15Qxl24SOn6arfXgAqGH0N1VthxvDIyrK2dZBfu1';
    $consumer_key = 'mU6nzH298ZoZCdYaqbyzA';
    $consumer_secret = 'gIDEYBiruLf29VEq7Zx75U7bFJrkia9HV8SSw0qjlI';

    $host = 'api.twitter.com';
    $method = 'GET';
    $path = '/1.1/search/tweets.json';  // api call path api.twitter.com/1.1/search/tweets.json

    $query = array( // query parameters
    'q' => '%23bookmyshow',
    'count' => '2'
    );

    $oauth = array(
    'q' => '%23bookmyshow',
    'count' => 2,
    'oauth_consumer_key' => $consumer_key,
    'oauth_nonce' => time(),
    'oauth_signature_method' => 'HMAC-SHA1',
    'oauth_token' => $oauth_access_token,
    'oauth_timestamp' => time(),
    'oauth_version' => '1.0'
    );



    $oauth = array_map("rawurlencode", $oauth); // must be encoded before sorting
    $query = array_map("rawurlencode", $query);

    $arr = array_merge($oauth, $query); // combine the values THEN sort

    asort($arr); // secondary sort (value)
    ksort($arr); // primary sort (key)

    // http_build_query automatically encodes, but our parameters
    // are already encoded, and must be by this point, so we undo
    // the encoding step
    $querystring = urldecode(http_build_query($arr, '', '&'));

    $url = "https://$host$path";

    // mash everything together for the text to hash
    $base_string = $method."&".rawurlencode($url)."&".rawurlencode($querystring);

    // same with the key
    $key = rawurlencode($consumer_secret)."&".rawurlencode($token_secret);

    // generate the hash
    $signature = rawurlencode(base64_encode(hash_hmac('sha1', $base_string, $key, true)));

    // this time we're using a normal GET query, and we're only encoding the query params
    // (without the oauth params)
    $url .= "?".http_build_query($query);

    $oauth['oauth_signature'] = $signature; // don't want to abandon all that work!
    ksort($oauth); // probably not necessary, but twitter's demo does it

    // also not necessary, but twitter's demo does this too
    function add_quotes($str) { return '"'.$str.'"'; }
    $oauth = array_map("add_quotes", $oauth);

    // this is the full value of the Authorization line
    $auth = "OAuth " . urldecode(http_build_query($oauth, '', ', '));
    $options = array(
    CURLOPT_HTTPHEADER => array("Authorization: $auth"),
    //CURLOPT_POSTFIELDS => $postfields,
    CURLOPT_HEADER => false,
    CURLOPT_URL => $url . '?q=%23bookmyshow&count=2', 
    CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false
    );
    // do our business
    $feed = curl_init();
    curl_setopt_array($feed, $options);
    $json = curl_exec($feed);
    curl_close($feed);

    $twitter_data = json_decode($json);
    //print_r($twitter_data);
    echo "<pre>";
    print_r(json_decode($json));

?>

Этот файл TwitterAPIExchange.php Я получил от Github.

1 ответ

Решение

Не требуется использовать %23 в поисковом запросе для поиска значений `.

Вместо 'q' => '%23bookmyshow'использовать 'q' => 'bookmyshow',

Кроме того, вы не просили Twitter получать твиты. Прочитайте эту документацию. Если это ваш секрет токена, я бы посоветовал вам сбросить ключи прямо сейчас. Перейдите на страницу Twitter для разработчиков, чтобы получить доступ к своим приложениям и сбросить их.

Другие вопросы по тегам