-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.php
More file actions
83 lines (66 loc) · 2.15 KB
/
example.php
File metadata and controls
83 lines (66 loc) · 2.15 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
require 'src/CurlMulti.php';
$handles = [
[
CURLOPT_URL=>"http://example.com/",
CURLOPT_HEADER=>false,
CURLOPT_FOLLOWLOCATION=>false,
CURLOPT_WRITEFUNCTION=>function($ch, $body)
{
print $body;
return strlen($body);
}
],
[
CURLOPT_URL=>"httpzzz://example.com/",
CURLOPT_HEADER=>false,
CURLOPT_FOLLOWLOCATION=>false,
CURLOPT_WRITEFUNCTION=>function($ch, $body)
{
print $body;
return strlen($body);
}
],
[
CURLOPT_URL=>"http://www.php.net",
CURLOPT_HEADER=>false,
CURLOPT_FOLLOWLOCATION=>false,
// this function is called by curl for each header received
// This complies with RFC822 and RFC2616, please do not suggest edits to make use of the mb_ string functions, it is incorrect!
// https://stackoverflow.com/a/41135574
CURLOPT_HEADERFUNCTION=>function($ch, $header)
{
print "header from http://www.php.net: ".$header;
return strlen($header);
},
CURLOPT_WRITEFUNCTION=>function($ch, $body)
{
print $body;
return strlen($body);
}
]
];
//create the multiple cURL handle
$CurlMulti = new TH\CurlMulti\CurlMulti();
foreach($handles as $opts) {
// create cURL resources
$ch = curl_init();
// set URL and other appropriate options
curl_setopt_array($ch, $opts);
// add the handle
$CurlMulti->add($ch, function($ch, $statusCode) {
$info = curl_getinfo($ch);
if ($statusCode !== CURLE_OK) {
// TODO: handle the error
print "Curl handle error: ".curl_strerror($statusCode)." for ".$info['url'].PHP_EOL;
return;
}
print_r($info);
$body = curl_multi_getcontent($ch);
echo $body;
});
}
$statusCode = $CurlMulti->run();
if ($statusCode !== CURLM_OK) {
print "Curl multi handle error: ".curl_multi_strerror($statusCode)." for ".$info['url'].PHP_EOL;
}