From 2a228e8ee45de8a15ecdb9e6067d9366fc2a89d4 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Tue, 6 Feb 2024 15:55:20 +0100 Subject: [PATCH] Add http curl driver --- inc/driver/http-driver.php | 113 +++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 inc/driver/http-driver.php diff --git a/inc/driver/http-driver.php b/inc/driver/http-driver.php new file mode 100644 index 00000000..659099ed --- /dev/null +++ b/inc/driver/http-driver.php @@ -0,0 +1,113 @@ +inner); + curl_setopt_array($this->inner, array( + CURLOPT_USERAGENT => $this->user_agent, + CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS, + )); + } + + function __construct($timeout, $user_agent) { + $this->inner = curl_init(); + $this->timeout = $timeout; + $this->user_agent = $user_agent; + } + + function __destruct() { + curl_close($this->inner); + } + + /** + * Execute a GET request. + * + * @param string $endpoint Uri endpoint. + * @param array|null $data Optional GET parameters. + * @return string Returns the body of the response. + * @throws Exception Throws on error. + */ + public function send_get($endpoint, $data) { + if (is_array($data) && !empty($data)) { + $endpoint .= '?' . http_build_query($data); + } + + $this->reset(); + curl_setopt($this->inner, CURLOPT_URL, $endpoint); + curl_setopt($this->inner, CURLOPT_RETURNTRANSFER, true); + curl_setopt($this->inner, CURLOPT_TIMEOUT, $this->timeout); + $ret = curl_exec($this->inner); + + if ($ret === false) { + throw new Exception(curl_error($this->inner)); + } + return $ret; + } + + /** + * Execute a POST request. + * + * @param string $endpoint Uri endpoint. + * @param array|null $data Optional POST parameters. + * @return string Returns the body of the response. + * @throws Exception Throws on error. + */ + public function send_post($endpoint, $data) { + $this->reset(); + curl_setopt($this->inner, CURLOPT_URL, $endpoint); + curl_setopt($this->inner, CURLOPT_POST, true); + if (is_array($data) && !empty($data)) { + curl_setopt($this->inner, CURLOPT_POSTFIELDS, http_build_query($data)); + } + curl_setopt($this->inner, CURLOPT_RETURNTRANSFER, true); + curl_setopt($this->inner, CURLOPT_TIMEOUT, $this->timeout); + $ret = curl_exec($this->inner); + + if ($ret === false) { + throw new Exception(curl_error($this->inner)); + } + return $ret; + } + + /** + * Download the url's target with curl. + * + * @param string $url Url to the file to download. + * @param File $fd File descriptor to save the content to. + * @param int $timeout Optional request timeout in seconds. Use the default timeout if 0. + * @return void + * @throws Exception Throws on error. + */ + public function send_get_into($endpoint, $fd, $timeout = 0) { + if ($timeout == 0) { + $timeout = $this->timeout; + } + + $this->reset(); + curl_setopt($this->inner, CURLOPT_URL, $endpoint); + curl_setopt($this->inner, CURLOPT_FAILONERROR, true); + curl_setopt($this->inner, CURLOPT_FOLLOWLOCATION, false); + curl_setopt($this->inner, CURLOPT_FILE, $fd); + curl_setopt($this->inner, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); + curl_setopt($this->inner, CURLOPT_TIMEOUT, $timeout); + $ret = curl_exec($this->inner); + + if ($ret === false) { + throw new Exception(curl_error($this->inner)); + } + } +}