From a042fbf56056d40c6c33f25355a65d2b7250e0b1 Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Wed, 9 Aug 2023 00:03:12 +0200 Subject: [PATCH] fix curl instead of HttpClient to fix server fails --- src/Utils.php | 48 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/src/Utils.php b/src/Utils.php index 585a9f3..239078b 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -188,28 +188,46 @@ class Utils self::write($contents); + $client = false; $status = 500; $response = ''; $url = sprintf(self::url(), 'report'); - $path = ''; - if ($client = HttpClient::initClient($url, $path)) { - try { - $client->setUserAgent('Dotclear.watch ' . My::id() . '/' . self::DISTANT_API_VERSION); - $client->useGzip(false); - $client->setPersistReferers(false); - $client->post($path, ['key' => self::key(), 'report' => $contents]); - $status = $client->getStatus(); - $response = $client->getContent(); - unset($client); - if ($status != 202) { - self::error((string) '(' . $status . ') ' . $response); + try { + if (function_exists('curl_init')) { + if (false !== ($client = curl_init($url))) { + curl_setopt($client, CURLOPT_RETURNTRANSFER, true); + curl_setopt($client, CURLOPT_POST, true); + curl_setopt($client, CURLOPT_POSTFIELDS, ['key' => self::key(), 'report' => $contents]); + + if (false !== ($response = curl_exec($client))) { + $status = (int) curl_getinfo($client, CURLINFO_HTTP_CODE); + } } + } else { + $path = ''; + if (false !== ($client = HttpClient::initClient($url, $path))) { + $client->setUserAgent('Dotclear.watch ' . My::id() . '/' . self::DISTANT_API_VERSION); + $client->useGzip(false); + $client->setPersistReferers(false); + $client->post($path, ['key' => self::key(), 'report' => $contents]); - return; - } catch (Exception $e) { - unset($client); + $status = (int) $client->getStatus(); + $response = $client->getContent(); + } } + + unset($client); + } catch (Exception $e) { + unset($client); + } + + if ($status == 202) { + return; + } + + if ($status !== false) { + self::error((string) '(' . $status . ') ' . $response); } if ($force) {