2017 © Pedro Peláez
 

library cowitter

Asynchronous Twitter client compatible with mpyw/co Generator-based flows.

image

kawakami-o3/cowitter

Asynchronous Twitter client compatible with mpyw/co Generator-based flows.

  • Thursday, February 8, 2018
  • by kawakami-o3
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 20 Forks
  • 0 Open issues
  • 8 Versions
  • 0 % Grown

The README.md

NOTE, (*1)

This project is forked for my own use. The dependency is less restrictive than the original project's one, and it maybe cause a problem which I will NOT fix., (*2)

cowitter Build Status Code Coverage Scrutinizer Code Quality

Asynchronous Twitter client compatible with mpyw/co Generator-based flows., (*3)

PHP :question: Feature Restriction
7.0~ :smile: Full Support
5.5~5.6 :anguished: Generator is not so cool
~5.4 :boom: Incompatible

Installing

composer require mpyw/cowitter:^1.0

Tutorial

  1. Preparation
  2. Example: Application for your own personal use
  3. Example: Sign in with Twitter
  4. Example: Commandline streaming readers

Quick examples

Prepare requirements

require __DIR__ . '/vendor/autoload.php';

use mpyw\Co\Co;
use mpyw\Co\CURLException;
use mpyw\Cowitter\Client;
use mpyw\Cowitter\HttpException;

Create client

$client = new Client(['CK', 'CS', 'AT', 'ATS']);

Synchronous requests

// Search tweets
$statuses = $client->get('search/tweets', ['q' => 'cowitter'])->statuses;
var_dump($statuses);
// Update tweet
$client->post('statuses/update', ['status' => 'Cowitter is the best twitter library for PHP!']);
// Update tweet with multiple images
$ids = [
    $client->postMultipart('media/upload', ['media' => new \CURLFile('photo01.png')])->media_id_string,
    $client->postMultipart('media/upload', ['media' => new \CURLFile('photo02.jpg')])->media_id_string,
];
$client->post('statuses/update', [
    'status' => 'My photos',
    'media_ids' => implode(',', $ids),
]);
// Listen user streaming
$client->streaming('user', function ($status) {
    if (!isset($status->text)) return;
    printf("%s(@s) - %s\n",
        $status->user->name,
        $status->user->screen_name,
        htmlspecialchars_decode($status->text, ENT_NOQUOTES)
    );
});

Asynchronous requests

// Search tweets
Co::wait(function () use ($client) {
    $statuses = (yield $client->getAsync('search/tweets', ['q' => 'cowitter']))->statuses;
    var_dump($statuses);
});
// Rapidly update tweets for 10 times
$tasks = [];
for ($i = 0; $i < 20; ++$i) {
    $tasks[] = $client->postAsync('statuses/update', [
        'status' => str_repeat('!', $i + 1),
    ]);
}
Co::wait($tasks);
// Rapidly update tweet with multiple images
Co::wait(function () use ($client) {
    $info = yield [
        $client->postMultipartAsync('media/upload', ['media' => new \CURLFile('photo01.png')]),
        $client->postMultipartAsync('media/upload', ['media' => new \CURLFile('photo02.png')]),
    ];
    yield $client->postAsync('statuses/update', [
        'status' => 'My photos',
        'media_ids' => implode(',', array_column($info, 'media_id_string')),
    ]);
});
// Listen filtered streaming to favorite/retweet at once each tweet
Co::wait($client->streamingAsync('statuses/filter', function ($status) use ($client) {
    if (!isset($status->text)) return;
    printf("%s(@s) - %s\n",
        $status->user->name,
        $status->user->screen_name,
        htmlspecialchars_decode($status->text, ENT_NOQUOTES)
    );
    yield Co::SAFE => [ // ignore errors
        $client->postAsync('favorites/create', ['id' => $status->id_str]),
        $client->postAsync("statuses/retweet/{$status->id_str}"),
    ];
}, ['track' => 'PHP']));
// Rapidly update with MP4 video
Co::wait(function () use ($client) {
    $file = new \SplFileObject('video.mp4', 'rb');
    $on_uploading = function ($percent) {
        echo "Uploading ... ({$percent}%)\n";
    };
    $on_processing = function ($percent) {
        echo "Processing ... ({$percent}%)\n";
    };
    yield $client->postAsync('statuses/update', [
        'status' => 'My video',
        'media_ids' => (yield $client->uploadVideoAsync($file, $on_uploading, $on_processing))->media_id_string,
    ]);
    echo "Done\n";
});

Handle exceptions

try {

    // do stuff here
    $client->get(...);
    $client->post(...);

} catch (HttpException $e) {

    // cURL communication successful but something went wrong with Twitter APIs.
    $message = $e->getMessage();    // Message
    $code    = $e->getCode();       // Error code (-1 if not available)
    $status  = $e->getStatusCode(); // HTTP status code

} catch (CURLException $e) {

    // cURL communication failed.
    $message = $e->getMessage();    // Message    (equivalent to curl_error())
    $code    = $e->getCode();       // Error code (equivalent to curl_errno())

}

or, (*4)

try {

    // do stuff here
    $client->get(...);
    $client->post(...);

} catch (\RuntimeException $e) {

    // Something failed.
    $message = $e->getMessage();

}

Avoiding SSL errors due to the old libcurl version

If you encountered SSL certificate problem error..., (*5)

  1. Download the latest cacert.pem from official libcurl site.
    https://curl.haxx.se/docs/caextract.html
  2. Please choose either of the following solutions.

2-A: Configure globally

Specify the path as curl.cainfo in your php.ini., (*6)

curl.cainfo="C:\foo\bar\baz\cacert.pem"

DO NOT forget restarting Apache., (*7)

2-B: Configure locally

Specify the path as CURLOPT_CAINFO. Using the magic constant __DIR__ is recommended., (*8)

$client = new Client(['CK', 'CS', 'AT', 'ATS'], [CURLOPT_CAINFO => __DIR__ . '/cacert.pem']);

or, (*9)

$client = new Client(['CK', 'CS', 'AT', 'ATS']);
$client = $client->withOptions([CURLOPT_CAINFO => __DIR__ . '/cacert.pem']);

Details

Read interfaces., (*10)

Todos

  • Documentation
  • Improving codes

The Versions

08/02 2018

dev-master

9999999-dev

Asynchronous Twitter client compatible with mpyw/co Generator-based flows.

  Sources   Download

MIT

The Requires

 

The Development Requires

by kawakami

api oauth upload image asynchronous video async twitter streaming paralell

13/12 2017

v1.0.3.2

1.0.3.2

Asynchronous Twitter client compatible with mpyw/co Generator-based flows.

  Sources   Download

MIT

The Requires

 

The Development Requires

by kawakami

api oauth upload image asynchronous video async twitter streaming paralell

13/12 2017

v1.0.3.1

1.0.3.1

Asynchronous Twitter client compatible with mpyw/co Generator-based flows.

  Sources   Download

MIT

The Requires

  • php >=5.5.0
  • ext-hash *
  • ext-json *
  • ext-openssl *
  • lib-curl *
  • mpyw/co ^1.5

 

The Development Requires

by kawakami

api oauth upload image asynchronous video async twitter streaming paralell

06/05 2017

dev-feature/webhook-support

dev-feature/webhook-support

Asynchronous Twitter client compatible with mpyw/co Generator-based flows.

  Sources   Download

MIT

The Requires

  • php >=5.5.0
  • ext-hash *
  • ext-json *
  • ext-openssl *
  • lib-curl >=7.20.0
  • mpyw/co ^1.5

 

The Development Requires

api oauth upload image asynchronous video async twitter streaming paralell

01/01 2017

v1.0.3

1.0.3.0

Asynchronous Twitter client compatible with mpyw/co Generator-based flows.

  Sources   Download

MIT

The Requires

  • php >=5.5.0
  • ext-hash *
  • ext-json *
  • ext-openssl *
  • lib-curl >=7.20.0
  • mpyw/co ^1.5

 

The Development Requires

api oauth upload image asynchronous video async twitter streaming paralell

13/09 2016

v1.0.2

1.0.2.0

Asynchronous Twitter client compatible with mpyw/co Generator-based flows.

  Sources   Download

MIT

The Requires

  • php >=5.5.0
  • ext-hash *
  • ext-json *
  • ext-openssl *
  • lib-curl >=7.20.0
  • mpyw/co ^1.5

 

The Development Requires

api oauth upload image asynchronous video async twitter streaming paralell

28/08 2016

v1.0.1

1.0.1.0

Asynchronous Twitter client compatible with mpyw/co Generator-based flows.

  Sources   Download

MIT

The Requires

  • php >=5.5.0
  • ext-hash *
  • ext-json *
  • ext-openssl *
  • lib-curl >=7.20.0
  • mpyw/co ^1.5

 

The Development Requires

api oauth upload image asynchronous video async twitter streaming paralell

27/08 2016

v1.0.0

1.0.0.0

Asynchronous Twitter client compatible with mpyw/co Generator-based flows.

  Sources   Download

MIT

The Requires

  • php >=5.5.0
  • ext-hash *
  • ext-json *
  • ext-openssl *
  • lib-curl >=7.20.0
  • mpyw/co ^1.5

 

The Development Requires

api oauth upload image asynchronous video async twitter streaming paralell