Skip to content
Twexapi
English
Esc
↑↓navigate↵open⌘Jpreview
On this page

PHP SDK

Use the TwexAPI PHP SDK for tweet search, follower exports, and public reads in PHP 8.1+ applications.

Use the PHP SDK when you want typed request objects, named parameters, retries, and pagination helpers for TwexAPI REST workflows in PHP 8.1+ applications.

twexapi-dev/x-api-scraper-phpPHP SDK for TwexAPI public social data and approved account workflows. Independent third-party service.00
PHP task SDK call Save
Search tweets $sdk->search->advanced nextCursor
Read profile $sdk->users->getAbout screenName
List followers $sdk->users->followers->list cursor

Install

composer require twexapi/x-api-scraper:^0.1.0

Packagist is not published yet. Install from GitHub until it is:

{
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/twexapi-dev/x-api-scraper-php.git"
        }
    ],
    "require": {
        "twexapi/x-api-scraper": "0.1.0"
    }
}

Then run:

composer update

Source: twexapi-dev/x-api-scraper-php

Authenticate

export X_API_SCRAPER_KEY="YOUR_API_KEY"
declare(strict_types=1);

require 'vendor/autoload.php';

use XapiScraper\XapiScraper;
use XapiScraper\Models\Components;

$sdk = XapiScraper::builder()
    ->setSecurity(getenv('X_API_SCRAPER_KEY'))
    ->build();

$request = new Components\AdvancedSearchCursorQuery(
    searchTerms: ['from:elonmusk'],
    sortBy: 'Latest',
    nextCursor: '',
);

$response = $sdk->search->advanced(request: $request);

Basic example

Look up a profile and list followers:

$about = $sdk->users->getAbout(screenName: 'elonmusk');

$followers = $sdk->users->followers->list(
    request: new Components\FollowersFollowingV3Query(username: 'elonmusk')
);

Workflow: search tweets

$cursor = '';
$pageIndex = 0;

while (true) {
    $page = $sdk->search->advanced(
        request: new Components\AdvancedSearchCursorQuery(
            searchTerms: ['from:elonmusk AI'],
            sortBy: 'Latest',
            nextCursor: $cursor,
        )
    )->advancedSearchCursorResponse;

    foreach ($page->tweets ?? [] as $tweet) {
        $row = [
            'source' => 'twexapi.php.search',
            'tweet_id' => $tweet->id,
            'text' => $tweet->text,
            'page_index' => $pageIndex,
            'next_cursor' => $page->nextCursor,
        ];
        echo json_encode($row) . PHP_EOL;
    }

    if (!$page->hasNextPage || empty($page->nextCursor)) {
        break;
    }

    $cursor = $page->nextCursor;
    $pageIndex += 1;
}

Error handling

use XapiScraper\Models\Errors;

try {
    $sdk->search->advanced(request: $request);
} catch (Errors\HTTPValidationErrorThrowable $e) {
    throw $e;
} catch (Errors\APIException $e) {
    echo $e->statusCode, $e->body;
}

References

Was this page helpful?