| 1 |
<?php |
| 2 |
|
| 3 |
class bwp_multi_http |
| 4 |
{ |
| 5 |
private $requests = array(); |
| 6 |
private $timeout; |
| 7 |
|
| 8 |
public function __construct($timeout = 30) |
| 9 |
{ |
| 10 |
$this->timeout = $timeout; |
| 11 |
} |
| 12 |
|
| 13 |
public function addRequest($method, $url, $data = null) |
| 14 |
{ |
| 15 |
$this->requests[] = array( |
| 16 |
'method' => $method, |
| 17 |
'url' => $url, |
| 18 |
'data' => $data, |
| 19 |
); |
| 20 |
} |
| 21 |
|
| 22 |
public function execute() |
| 23 |
{ |
| 24 |
if (extension_loaded('curl')) { |
| 25 |
return $this->executeWithCurl(); |
| 26 |
} else { |
| 27 |
return $this->executeWithFileGetContents(); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
private function executeWithCurl() |
| 32 |
{ |
| 33 |
$responses = array(); |
| 34 |
$mh = curl_multi_init(); |
| 35 |
$handles = array(); |
| 36 |
|
| 37 |
foreach ($this->requests as $key => $request) { |
| 38 |
$ch = curl_init($request['url']); |
| 39 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 40 |
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); |
| 41 |
|
| 42 |
// Set custom User-Agent header |
| 43 |
curl_setopt($ch, CURLOPT_USERAGENT, 'BerqWP Bot'); |
| 44 |
|
| 45 |
if ($request['method'] === 'POST') { |
| 46 |
curl_setopt($ch, CURLOPT_POST, true); |
| 47 |
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($request['data'])); |
| 48 |
} |
| 49 |
|
| 50 |
// Disable SSL certificate verification |
| 51 |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
| 52 |
|
| 53 |
// Add handle to multi handle |
| 54 |
curl_multi_add_handle($mh, $ch); |
| 55 |
$handles[$key] = $ch; |
| 56 |
} |
| 57 |
|
| 58 |
$running = null; |
| 59 |
do { |
| 60 |
curl_multi_exec($mh, $running); |
| 61 |
} while ($running > 0); |
| 62 |
|
| 63 |
foreach ($handles as $key => $ch) { |
| 64 |
$response = curl_multi_getcontent($ch); |
| 65 |
if ($response === false) { |
| 66 |
$responses[] = array( |
| 67 |
'error' => 'Error fetching URL: ' . $this->requests[$key]['url'], |
| 68 |
); |
| 69 |
} else { |
| 70 |
$responses[] = $response; |
| 71 |
} |
| 72 |
curl_multi_remove_handle($mh, $ch); |
| 73 |
curl_close($ch); |
| 74 |
} |
| 75 |
|
| 76 |
curl_multi_close($mh); |
| 77 |
|
| 78 |
return $responses; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
private function executeWithFileGetContents() |
| 83 |
{ |
| 84 |
$responses = array(); |
| 85 |
|
| 86 |
foreach ($this->requests as $request) { |
| 87 |
$contextOptions = array( |
| 88 |
'http' => array( |
| 89 |
'method' => $request['method'], |
| 90 |
'timeout' => $this->timeout, |
| 91 |
'ignore_errors' => true, // Disables SSL certificate validation |
| 92 |
) |
| 93 |
); |
| 94 |
|
| 95 |
// Set custom User-Agent header |
| 96 |
$contextOptions['http']['header'] = "User-Agent: BerqWP Bot\r\n"; |
| 97 |
|
| 98 |
if ($request['method'] === 'POST') { |
| 99 |
$contextOptions['http']['content'] = http_build_query($request['data']); |
| 100 |
} |
| 101 |
|
| 102 |
$context = stream_context_create($contextOptions); |
| 103 |
$response = file_get_contents($request['url'], false, $context); |
| 104 |
|
| 105 |
if ($response === false) { |
| 106 |
$responses[] = array( |
| 107 |
'error' => 'Error fetching URL: ' . $request['url'], |
| 108 |
); |
| 109 |
} else { |
| 110 |
$responses[] = $response; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return $responses; |
| 115 |
} |
| 116 |
} |
| 117 |
|