From c1e47171797ecb42d225eec0792d71980f297447 Mon Sep 17 00:00:00 2001 From: Steffen Lange Date: Sun, 17 Jan 2021 23:22:20 +0100 Subject: [PATCH] Use cURL instead of PHP routine --- PatchBase.php | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/PatchBase.php b/PatchBase.php index 7317a0b..5d1e224 100644 --- a/PatchBase.php +++ b/PatchBase.php @@ -23,20 +23,31 @@ abstract class PatchBase { abstract function check() : bool; protected function fetch(string $url, bool $json = false) : bool { $host = parse_url($url, PHP_URL_HOST); - $opt = array('http' => array('user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:1.0) Gecko/20200101 Patchbot/1.0')); + /*$opt = array('http' => array('user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:1.0) Gecko/20200101 Patchbot/1.0')); if ($str = HostOption::get($host)) $opt['http'] += array('header' => $str); $ctx = stream_context_create($opt); - if ($str = @file_get_contents($url, false, $ctx)) { - $this->data = $str; - if ($json) { - if (!($this->data = json_decode($str, true))) - return false; - // get first element only - if ($this->array_isMulti($this->data)) - $this->data = array_shift($this->data); + if ($str = @file_get_contents($url, false, $ctx)) {*/ + if ($ch = curl_init()) { + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:1.0) Gecko/20200101 Patchbot/1.0'); + if ($opt = HostOption::get($host)) + curl_setopt($ch, CURLOPT_HTTPHEADER, $opt); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + $str = curl_exec($ch); + curl_close($ch); + if ($str) { + $this->data = $str; + if ($json) { + if (!($this->data = json_decode($str, true))) + return false; + // get first element only + if ($this->array_isMulti($this->data)) + $this->data = array_shift($this->data); + } + return true; } - return true; } return false; }