Skip to content

Latest commit

 

History

History
193 lines (157 loc) · 13.4 KB

twitter.md

File metadata and controls

193 lines (157 loc) · 13.4 KB

Twitter Client

The library has been designed to send request to the Twitter API. In order to do that, you will need a Twitter client. This one needs itself an OAuth client to sign request & an access token. If you want to learn more about this access token, you can read this documentation.

So, first, create your Twitter client:

use Widop\HttpAdapter\CurlHttpAdapter;
use Widop\Twitter\OAuth;
use Widop\Twitter\Rest\Statuses\StatusesUpdateRequest;
use Widop\Twitter\Rest\Twitter;

$oauth = new OAuth\OAuth(
    new CurlHttpAdapter(),
    new OAuth\OAuthConsumer('consumer_key', 'consumer_secret'),
    new OAuth\Signature\OAuthHmacSha1Signature()
);

$token = new OAuth\Token\OAuthToken('oauth_key', 'oauth_secret');

$twitter = new Twitter($oauth, $token);

Note that is also possible to pass a "bearer" token (eg: application access token):

use Widop\Twitter\OAuth\Token\BearerToken;

$twitter->setToken(new BearerToken('foo'));

Here, a simple example where we will destroy the tweet "123":

use Widop\Twitter\OAuth\OAuthException,
use Widop\Twitter\Rest\Statuses\StatusesDestroyRequest;

$request = new StatusesDestroyRequest('123');

try {
    $response = $twitter->send($request);
} catch (OAuthException $e) {
    // Something goes wrong.
} catch (\Exception $e) {
    // Something goes very wrong.
}

The send method returns a Widop\Twitter\OAuth\OAuthResponse which gives you access to the decoded response data, the underlying http response, the current rate limits and more:

$httpResponse = $response->getHttpResponse();

$data = $response->getData();
$id = $response->getData('id');

$limit = $response->getRateLimitLimit();
$remaining = $response->getRateLimitRemaining();
$reset = $response->getRateLimitReset();

$format = $response->getFormat();

Additionally, it allows you to send all the available requests which are listed here:

Obviously, if you want to complete the list, you're welcome :)