forked from reactphp/reactphp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp-client.php
More file actions
40 lines (30 loc) · 1.11 KB
/
Copy pathhttp-client.php
File metadata and controls
40 lines (30 loc) · 1.11 KB
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
<?php
// http client making a request to github api
require __DIR__.'/../vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$dnsResolverFactory = new React\Dns\Resolver\Factory();
$dnsResolver = $dnsResolverFactory->createCached('8.8.8.8', $loop);
$factory = new React\HttpClient\Factory();
$client = $factory->create($loop, $dnsResolver);
$request = $client->request('GET', 'https://api.github.com/repos/reactphp/react/commits');
$request->on('response', function ($response) {
$buffer = '';
$response->on('data', function ($data) use (&$buffer) {
$buffer .= $data;
echo ".";
});
$response->on('end', function () use (&$buffer) {
$decoded = json_decode($buffer, true);
$latest = $decoded[0]['commit'];
$author = $latest['author']['name'];
$date = date('F j, Y', strtotime($latest['author']['date']));
echo "\n";
echo "Latest commit on react was done by {$author} on {$date}\n";
echo "{$latest['message']}\n";
});
});
$request->on('end', function ($error, $response) {
echo $error;
});
$request->end();
$loop->run();