2017 © Pedro Peláez
 

library zendservice-twitter

OOP wrapper for the Twitter web service

image

zendframework/zendservice-twitter

OOP wrapper for the Twitter web service

  • Monday, May 14, 2018
  • by zendframework
  • Repository
  • 21 Watchers
  • 22 Stars
  • 81,801 Installations
  • PHP
  • 8 Dependents
  • 0 Suggesters
  • 29 Forks
  • 3 Open issues
  • 18 Versions
  • 5 % Grown

The README.md

zendservice-twitter

Repository abandoned 2019-12-31

This repository has moved to laminas/laminas-twitter., (*1)

Build Status Coverage Status, (*2)

Provides an object oriented PHP wrapper for the Twitter API., (*3)

Installation

Run the following to install this library:, (*4)

$ composer require zendframework/zendservice-twitter

Usage

Instantiate the Twitter class by providing your Twitter consumer key and secret, as well as the access token and secret:, (*5)

use ZendService\Twitter\Twitter;

$twitter = new Twitter([
    'access_token' => [
        'token' => '<token value>',
        'secret' => '<token secret value>',
    ],
    'oauth_options' => [
        'consumerKey' => '<consumer key value>',
        'consumerSecret' => '<consumer secret value>',
    ],
]);

Once you have done that, you may start making calls to the API. This can be done in one of three ways:, (*6)

  • Using direct method calls on the Twitter class. A full list is provided below.
  • Using the "proxy" functionality. In these cases, you will provide the first path element of the API, and then call a method on it: $twitter->statuses->update($message).
  • Using the get() or post() methods.

Available methods

  • accountVerifyCredentials() : Response
  • applicationRateLimitStatus() : Response
  • blocksCreate($id) : Response
  • blocksDestroy($id) : Response
  • blocksIds(int $cursor = -1) : Response
  • blocksList(int $cursor = -1) : Response
  • directMessagesDestroy($id) : Response
  • directMessagesMessages(array $options = []) : Response
  • directMessagesNew($user, string $text, array $extraParams = []) : Response
  • directMessagesEventsNew($user, string $text, array $extraParams = []) : Response
  • directMessagesSent(array $options = []) : Response
  • favoritesCreate($id) : Response
  • favoritesDestroy($id) : Response
  • favoritesList(array $options = []) : Response
  • followersIds($id, array $params = []) : Response
  • friendsIds($id, array $params = []) : Response
  • friendshipsCreate($id, array $params = []) : Response
  • friendshipsLookup($id, array $params = []) : Response
  • friendshipsDestroy($id) : Response
  • listsMembers($listIdOrSlug, array $params = []) : Response
  • listsMemberships($id, array $params = []) : Response
  • listsSubscribers($id, array $params = []) : Response
  • searchTweets(string $query, array $options = []) : Response
  • statusesDestroy($id) : Response
  • statusesHomeTimeline(array $options = []) : Response
  • statusesMentionsTimeline(array $options = []) : Response
  • statusesSample() : Response
  • statusesShow($id, array $options = []) : Response
  • statusesUpdate(string $status, $inReplyToStatusId = null, $extraAttributes = []) : Response
  • statusesUserTimeline(array $options = []) : Response
  • usersLookup($id, array $params = []) : Response
  • usersSearch(string $query, array $options = []) : Response
  • usersShow($id) : Response

Proxy Properties

The following proxy properties are allowed:, (*7)

  • account
  • application
  • blocks
  • directmessages
  • favorites
  • followers
  • friends
  • friendships
  • lists
  • search
  • statuses
  • users

In each case, you can identify available methods for the proxy by comparing the proxy name to the above list of methods. As an example, the users proxy allows the following:, (*8)

$twitter->users->lookup($id, array $params = []);
$twitter->users->search(string $query, array $options = []);
$twitter->users->show($id);

Direct access

The Twitter API has dozens of endpoints, some more popular and/or useful than others. As such, we are only providing a subset of what is available., (*9)

However, we allow you to access any endpoint via either the get() or post() methods, which have the following signatures:, (*10)

public function get(string $path, array $query = []) : Response;
public function post(string $path, $data = null) : Response;

In each case, the $path is the API endpoint as detailed in the Twitter API documentation, minus any .json suffix, and the method name corresponds to whether the request happens via HTTP GET or POST., (*11)

For HTTP GET requests, the $query argument provides any query string parameters you want to pass for that endpoint. As an example, if you were requesting statuses/home_timeline, you might pass count or since_id., (*12)

For HTTP POST requests, the $data argument can be one of:, (*13)

  • An associative array of data.
  • A serializable object of data.
  • A string representing the raw payload.

The data to provide will vary based on the endpoint., (*14)

Media uploads

Since version 3.0, we have supported media uploads via the classes ZendService\Twitter\Media, Image, and Video. In each case, you will instantiate the appropriate class with the local filesystem path of the image to upload and the media type, followed by calling upload() with a properly configured HTTP client. The response will contain a media_id property, which you can then provide via the media_ids parameter when posting a status:, (*15)

$image = new Image('data/logo.png', 'image/png');
$response = $image->upload($twitter->getHttpClient());

$twitter->statusUpdate(
    'A post with an image',
    null,
    ['media_ids' => [$response->media_id]]
);

When providing media for direct messages, you must provide additional flags to the media class's constructor:, (*16)

  • A flag indicating it is for a direct message
  • A flag indicating whether or not the uploaded media may be shared/re-used in other direct messages.
$image = new Image(
    'data/logo.png',
    'image/png',
    $forDirectMessage = true,
    $shared = false
);
$upload = $image->upload($twitter->getHttpClient());

Unlike non-DM media uploads, the identifier will be in the id_str parameter of the returned upload instance; use that as a media_id in your DM:, (*17)

$twitter->directmessagesEventsNew(
    $user,
    $message,
    ['media_id' => $upload->id_str]
);

Note: direct messages only support a single attachment., (*18)

Rate limiting

As of version 3.0, we now provide introspection of Twitter's rate limit headers, allowing you to act on them:, (*19)

$response = $twitter->statusUpdate('A post');
$rateLimit = $response->getRateLimit();
if ($rateLimit->remaining === 0) {
    // Time to back off!
    sleep($rateLimit->reset); // seconds left until reset
}

The Versions

24/09 2012

2.0.1

2.0.1.0 http://packages.zendframework.com/

OOP wrapper for the Twitter web service

  Sources   Download

BSD-3-Clause

The Requires

 

zf2 twitter

23/07 2012

2.0.0

2.0.0.0 http://packages.zendframework.com/

OOP wrapper for the Twitter web service

  Sources   Download

BSD-3-Clause

The Requires

 

zf2 twitter

23/07 2012

2.0.0rc1

2.0.0.0-RC1 http://packages.zendframework.com/

OOP wrapper for the Twitter web service

  Sources   Download

BSD-3-Clause

The Requires

 

zf2 twitter

23/07 2012

2.0.0rc2

2.0.0.0-RC2 http://packages.zendframework.com/

OOP wrapper for the Twitter web service

  Sources   Download

BSD-3-Clause

The Requires

 

zf2 twitter

23/07 2012

2.0.0rc3

2.0.0.0-RC3 http://packages.zendframework.com/

OOP wrapper for the Twitter web service

  Sources   Download

BSD-3-Clause

The Requires

 

zf2 twitter

23/07 2012

2.0.0rc4

2.0.0.0-RC4 http://packages.zendframework.com/

OOP wrapper for the Twitter web service

  Sources   Download

BSD-3-Clause

The Requires

 

zf2 twitter

23/07 2012

2.0.0rc5

2.0.0.0-RC5 http://packages.zendframework.com/

OOP wrapper for the Twitter web service

  Sources   Download

BSD-3-Clause

The Requires

 

zf2 twitter

23/07 2012

2.0.0rc6

2.0.0.0-RC6 http://packages.zendframework.com/

OOP wrapper for the Twitter web service

  Sources   Download

BSD-3-Clause

The Requires

 

zf2 twitter

23/07 2012

2.0.0rc7

2.0.0.0-RC7 http://packages.zendframework.com/

OOP wrapper for the Twitter web service

  Sources   Download

BSD-3-Clause

The Requires

 

zf2 twitter