-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.php
50 lines (39 loc) · 1.63 KB
/
stream.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php declare(strict_types = 1);
/**
* @author Adam Englander <adamenglander@yahoo.com>
* @copyright 2017 Adam L. Englander. See project license for usage.
*/
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/keys.php';
use Amp\Artax\Client as ArtaxHttpClient;
use PeeHaa\AsyncTwitter\Api\Request\Stream\Filter;
use PeeHaa\AsyncTwitter\Credentials\AccessToken;
use PeeHaa\AsyncTwitter\Credentials\Application;
use PeeHaa\AsyncTwitter\Api\Client\Client as ApiClient;
use PeeHaa\AsyncTwitter\Http\Artax as ApiHttpClient;
// Create application credentials object for Twitter client
$applicationCredentials = new Application($key, $secret);
// Create token credentials object for Twitter client
$accessToken = new AccessToken($token, $tokenSecret);
// Create a new API HTTP Client with the Artax HTTP client
$httpClient = new ApiHttpClient(new ArtaxHttpClient());
// Create an API client with the API HTTP client and credentials
$apiClient = new ApiClient($httpClient, $applicationCredentials, $accessToken);
// Make a filtered user stream request that tracks keywords
$request = (new Filter)
->track(["pkptek", "phptek2017"]);
// Have Amp run the following
\Amp\run(function () use ($apiClient, $request) {
// Open a stream
$stream = yield $apiClient->request($request);
// Loop through stream responses until there are none
while (null !== $message = yield $stream->read()) {
// Print the user's screen name and update text
print(sprintf(
"(%s)\n%s",
$message['user']['screen_name'],
$message['text']
));
print("\n==================\n");
}
});