-
Notifications
You must be signed in to change notification settings - Fork 4
/
p2-async.php
executable file
·54 lines (39 loc) · 1.5 KB
/
p2-async.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
51
52
53
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
require('phpQuery/phpQuery.php');
require('vendor/autoload.php');
$time_start = microtime(true);
$total_page = 200;
$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);
echo "Page number, Time taken";
for ($page = 1; $page <= $total_page; $page++) {
$loop->addTimer(0.001, function($timer) use ($client, $page) {
$buffer = '';
$request = $client->request('GET', 'http://fantasy.premierleague.com/my-leagues/303/standings/?ls-page='.$page);
$request->on('response', function($response) use (&$buffer) {
$response->on('data', function($data) use (&$buffer) {
$buffer .= $data;
});
});
$request->on('end', function() use (&$buffer, $page) {
\phpQuery::newDocument($buffer);
foreach (pq('.ismStandingsTable tr') as $data) {
foreach (pq('td', $data) as $key => $val) {
if ($key == 2) {
// print pq($val)->text();
}
}
}
$time_end = microtime(true);
$execution_time = $time_end - $GLOBALS['time_start'];
echo ("\n".$page.", ".$execution_time);
});
$request->end();
});
}
$loop->run();