|
| 1 | +<?php |
| 2 | + |
| 3 | +use ApiClients\Client\Twitter\AsyncClient; |
| 4 | +use ApiClients\Client\Twitter\Resource\Async\Tweet; |
| 5 | +use ApiClients\Client\Twitter\Resource\ProfileInterface; |
| 6 | +use React\EventLoop\Factory; |
| 7 | +use function ApiClients\Foundation\resource_pretty_print; |
| 8 | + |
| 9 | +if (!isset($argv[1])) { |
| 10 | + echo 'This example requires you to pass a username for example \'php profile-update-name-async.php "Cees-Jan %s Kiewiet"\'.', PHP_EOL; |
| 11 | + echo 'The %s in there will be replaced with a random emoji.', PHP_EOL; |
| 12 | + exit(255); |
| 13 | +} |
| 14 | + |
| 15 | +require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor/autoload.php'; |
| 16 | +$config = require 'resolve_config.php'; |
| 17 | +$emojis = require 'emoji.php'; |
| 18 | + |
| 19 | +$loop = Factory::create(); |
| 20 | +$client = (new AsyncClient( |
| 21 | + $config['consumer']['key'], |
| 22 | + $config['consumer']['secret'], |
| 23 | + $loop |
| 24 | +))->withAccessToken( |
| 25 | + $config['access_token']['token'], |
| 26 | + $config['access_token']['secret'] |
| 27 | +)->profile()->done(function (ProfileInterface $profile) use ($client, $argv, $emojis) { |
| 28 | + resource_pretty_print($profile); |
| 29 | + $client->stream()->filtered([ |
| 30 | + 'follow' => $profile->idStr(), |
| 31 | + ])->filter(function ($resource) { |
| 32 | + return $resource instanceof Tweet; |
| 33 | + })->filter(function (Tweet $tweet) use ($profile) { |
| 34 | + return $tweet->user()->idStr() === $profile->idStr(); |
| 35 | + })->subscribeCallback(function (Tweet $tweet) use ($profile, $argv, $emojis) { |
| 36 | + echo '------------------', PHP_EOL; |
| 37 | + echo $tweet->text(), PHP_EOL; |
| 38 | + echo '------------------', PHP_EOL; |
| 39 | + $profile = $profile->withName(sprintf( |
| 40 | + $argv[1], |
| 41 | + $emojis[random_int(0, count($emojis) - 1)] |
| 42 | + )); |
| 43 | + $profile->putProfile()->done(function (ProfileInterface $newProfile) use ($profile) { |
| 44 | + $profile = $newProfile; |
| 45 | + resource_pretty_print($profile); |
| 46 | + }); |
| 47 | + }); |
| 48 | +}); |
| 49 | + |
| 50 | +$loop->run(); |
0 commit comments