Stars: 548
Forks: 168
Pull Requests: 40
Issues: 0
Watchers: 61
Last Updated: 2022-11-29 17:24:25
Small and easy PHP library for sending messages to Twitter and receiving statuses.
License: BSD 3-Clause "New" or "Revised" License
Languages: PHP
Twitter for PHP is a very small and easy-to-use library for sending messages to Twitter and receiving status updates.
It requires PHP 5.4 or newer with CURL extension and is licensed under the New BSD License. You can obtain the latest version from our GitHub repository or install it via Composer:
composer require dg/twitter-php
Do you like Nette DI? Are you looking forward to the new features?
Thank you!
Sign in to the https://twitter.com and register an application from the https://apps.twitter.com page. Remember to never reveal your consumer secrets. Click on My Access Token link from the sidebar and retrieve your own access token. Now you have consumer key, consumer secret, access token and access token secret.
Create object using application and request/access keys
use DG\Twitter\Twitter;
$twitter = new Twitter($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
The send() method updates your status. The message must be encoded in UTF-8:
$twitter->send('I am fine today.');
The load() method returns the 20 most recent status updates posted by you:
$statuses = $twitter->load(Twitter::ME);
or posted by you and your friends:
$statuses = $twitter->load(Twitter::ME_AND_FRIENDS);
or most recent mentions for you:
$statuses = $twitter->load(Twitter::REPLIES);
Extracting the information from the channel is easy:
foreach ($statuses as $status) {
echo "message: ", Twitter::clickable($status);
echo "posted at " , $status->created_at;
echo "posted by " , $status->user->name;
}
The static method Twitter::clickable()
makes links, mentions and hash tags in status clickable.
The authenticate() method tests if user credentials are valid:
if (!$twitter->authenticate()) {
die('Invalid name or password');
}
The search() method provides searching in twitter statuses:
$results = $twitter->search('#nette');
The returned result is a again array of statuses.
All methods throw a DG\Twitter\Exception
on error:
try {
$statuses = $twitter->load(Twitter::ME);
} catch (DG\Twitter\Exception $e) {
echo "Error: ", $e->getMessage();
}
The authenticate()
method tests if user credentials are valid:
if (!$twitter->authenticate()) {
die('Invalid name or password');
}
You can use all commands defined by Twitter API 1.1. For example GET statuses/retweets_of_me returns the array of most recent tweets authored by the authenticating user:
$statuses = $twitter->request('statuses/retweets_of_me', 'GET', ['count' => 20]);
v4.1 (11/2019)
v4.0 (2/2019)
v3.8 (2/2019)
v3.7 (3/2018)
v3.6 (8/2016)
v3.5 (12/2014)
v3.4 (11/2014)
v3.3 (3/2014)
v3.2 (1/2014)
v3.1 (3/2013)
v3.0 (12/2012)
v2.0 (8/2012)
composer require dg/twitter-php
v1.0 (7/2008)
(c) David Grudl, 2008, 2016 (https://davidgrudl.com)