You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Twitter for PHP](https://phpfashion.com/twitter-for-php)[](https://nette.org/make-donation?to=twitter-php)
2
-
================================
1
+
X for PHP
2
+
=========
3
3
4
4
[](https://packagist.org/packages/dg/twitter-php)
Twitter for PHP is a very small and easy-to-use library for sending
7
-
messages to Twitter and receiving status updates.
9
+
Small and easy-to-use library for the [X](https://x.com) (formerly Twitter) API v2.
8
10
9
-
It requires PHP 5.4 or newer with CURL extension and is licensed under the New BSD License.
10
-
You can obtain the latest version from our [GitHub repository](https://github.com/dg/twitter-php)
11
-
or install it via Composer:
11
+
<!---->
12
12
13
-
composer require dg/twitter-php
14
13
14
+
Installation
15
+
============
15
16
16
-
[Support Me](https://github.com/sponsors/dg)
17
-
--------------------------------------------
17
+
Install via Composer:
18
18
19
-
Do you like Nette DI? Are you looking forward to the new features?
19
+
```shell
20
+
composer require dg/twitter-php
21
+
```
20
22
21
-
[](https://github.com/sponsors/dg)
23
+
Requirements: PHP 8.2 or higher.
22
24
23
-
Thank you!
25
+
<!---->
26
+
27
+
28
+
Getting API Keys
29
+
================
30
+
31
+
1. Go to [X Developer Portal](https://developer.x.com)
32
+
2. Sign in and create a Developer account (Free tier is sufficient)
33
+
3. Create a new Project and App
34
+
35
+
4.**Set up permissions** (important!):
36
+
- App Settings → User authentication settings → Edit
37
+
- App permissions: **Read and Write**
38
+
- Type of App: Web App
39
+
- Callback URI: `https://localhost/callback` (placeholder, not used)
40
+
- Website URL: any valid URL
41
+
42
+
5.**Generate keys**:
43
+
- Keys and tokens → API Key and Secret → Generate
44
+
- Keys and tokens → Access Token and Secret → Generate
45
+
46
+
⚠️ **Note**: After changing permissions, you must regenerate the Access Token!
47
+
48
+
6.**Use case description** (required by X, min. 250 characters):
49
+
```
50
+
I am building a personal tool for publishing my own content to X. The
51
+
application will only be used to post text updates and images from my own
52
+
account. It will not read, collect, or analyze any data from other users.
53
+
There is no automation of likes, retweets, follows, or any other
54
+
interactions. This is strictly a single-user tool for my personal use to
55
+
streamline my social media posting workflow.
56
+
```
57
+
58
+
<!---->
24
59
25
60
26
61
Usage
27
-
-----
28
-
Sign in to the https://twitter.com and register an application from the https://apps.twitter.com page. Remember
29
-
to never reveal your consumer secrets. Click on My Access Token link from the sidebar and retrieve your own access
30
-
token. Now you have consumer key, consumer secret, access token and access token secret.
62
+
=====
31
63
32
-
Create object using application and request/access keys
64
+
Create the client using your API credentials:
33
65
34
66
```php
35
-
use DG\Twitter\Twitter;
67
+
use DG\X\Client;
36
68
37
-
$twitter = new Twitter($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
69
+
$x = new Client($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
38
70
```
39
71
40
-
The send() method updates your status. The message must be encoded in UTF-8:
72
+
73
+
**Note:** The Free tier of the X API only allows sending and deleting tweets and reading your own profile (`authenticate()`).
74
+
Reading timelines, searching, user info, followers and other read endpoints require the [Basic tier](https://developer.x.com/en/docs/x-api/getting-started/about-x-api) or higher.
75
+
76
+
77
+
### Sending Tweets
41
78
42
79
```php
43
-
$twitter->send('I am fine today.');
80
+
$x->sendTweet('I am fine today.');
44
81
```
45
82
46
-
The load() method returns the 20 most recent status updates
47
-
posted by you:
83
+
With an image:
48
84
49
85
```php
50
-
$statuses = $twitter->load(Twitter::ME);
86
+
$x->sendTweet('Check this out!', '/path/to/image.jpg');
0 commit comments