> ## Documentation Index
> Fetch the complete documentation index at: https://docs.twexapi.io/llms.txt
> Use this file to discover all available pages before exploring further.

# PHP SDK

> Use the TwexAPI PHP SDK for tweet search, follower exports, DMs, and X automation 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.

| 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

```bash theme={null}
composer require twexapi/x-api-scraper:^0.1.0
```

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

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

Then run:

```bash theme={null}
composer update
```

Source: [twexapi-dev/x-api-scraper-php](https://github.com/twexapi-dev/x-api-scraper-php)

## Authenticate

```bash theme={null}
export X_API_SCRAPER_KEY="YOUR_API_KEY"
```

```php theme={null}
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:

```php theme={null}
$about = $sdk->users->getAbout(screenName: 'elonmusk');

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

## Workflow: search tweets

```php theme={null}
$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;
}
```

## Workflow: post a tweet

Write actions need a Twitter cookie or `auth_token` on the request:

```php theme={null}
$sdk->tweets->actions->create(
    request: new Components\CreateTweetRequest(
        tweetContent: 'Hello from the TwexAPI PHP SDK.',
        cookie: getenv('TWITTER_COOKIE') ?: '',
    )
);
```

## Error handling

```php theme={null}
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

* [SDKs overview](/sdks)
* [Authentication](/authentication)
* [Advanced Twitter Search](/api-reference/search-endpoints/advanced-twitter-search)
* [Source Repository](https://github.com/twexapi-dev/x-api-scraper-php)
