Twitter search by hashtag example API v1.1

Twitter search by hashtag example API v1.1

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Migrating Twitter Search from API v1 to v1.1: A Practical Guide for PHP Developers

Retrieving a JSON feed of tweets based on a hashtag like #baseball using the legacy Twitter API v1 (search.twitter.com) was straightforward but limited. The URL structure allowed for simple GET requests without authentication, returning recent results directly in JSON format. However, transitioning to API version 1.1 fundamentally changed how you interact with Twitter data. You can no longer rely on open endpoints that lack security; every request now requires authentication via OAuth 1.0a.

This shift means your PHP application must handle token acquisition and management before querying the search endpoint. The most critical change involves understanding rate limits and access scopes. Unlike the older API, which often allowed unrestricted reads for public data, API v1.1 enforces strict quotas based on whether you are using standard authentication or elevated access. If your application is a high-volume scraper or requires access to specific restricted fields, you will need to apply for elevated access through Twitter's developer portal. Without this, your requests may be throttled significantly, causing your PHP script to fail when trying to fetch more than a few hundred tweets per 15-minute window.

To implement search functionality in API v1.1 with PHP, you must first obtain the necessary OAuth credentials: consumer key, consumer secret, access token, and access token secret. These are typically stored securely in an environment configuration file rather than hard-coded into your source. Once authenticated, you cannot simply hit a URL with a query string as you did in v1. The request becomes a POST operation to the statuses/search/tweets endpoint (or similar variations depending on the specific sub-endpoint version).

Here is a conceptual example of how this looks in PHP using the TwitterOAuth library, which simplifies the OAuth signing process:

require 'vendor/autoload.php';

use TwitterOAuth\TwitterOAuth;

$consumer_key = getenv('TWITTER_CONSUMER_KEY');
$consumer_secret = getenv('TWITTER_CONSUMER_SECRET');
$access_token = getenv('TWITTER_ACCESS_TOKEN');
$access_token_secret = getenv('TWITTER_ACCESS_TOKEN_SECRET');

$url = 'https://api.twitter.com/1.1/tweets/search/recent.json';

$client = new TwitterOAuth($consumer_key, $consumer_secret, $access_token, $access_token_secret);

// Define the query parameters
$params = [
    'q' => '#baseball',
    'result_type' => 'recent', // Note: This parameter was removed in newer v1.1 versions
];

$response = $client->post($url, [
    'query' => implode('&', array_map(function ($k, $v) { return "$k=$v"; }, array_keys($params), array_values($params)))
]);

// In reality, you would likely pass params directly to a dedicated method depending on the library implementation

Note that result_type=recent is often deprecated or ignored in strict v1.1 implementations, as the "recent" stream is now the default behavior for authenticated search requests. You must instead rely on pagination cursors found in the response headers (next_token, previous_token) to traverse results beyond the initial 100 tweets.

A common pitfall developers face during migration is attempting to replicate the exact JSON structure of API v1. The response format in v1.1 is more verbose and includes metadata about pagination that must be parsed manually. Additionally, the free tier of the API has strict rate limits; if your script hits these limits, you will receive a 429 Too Many Requests error.

For projects requiring robust data ingestion without managing complex OAuth rotation or hitting rate limits constantly, consider leveraging modern frameworks that abstract these concerns. Resources like https://laravelcompany.com often highlight how Laravel's service providers and client configurations can streamline third-party API interactions, reducing boilerplate code for authentication and error handling.

Ultimately, the migration from v1 to v1.1 is not just an API version swap; it is a shift toward a secured, rate-limited environment where proper token management and pagination logic are non-negotiable components of your architecture.

Our network