phpbb_ailabs/privet/ailabs/includes/GenericCurl.php

152 lines
3.8 KiB
PHP
Raw Normal View History

2023-05-28 09:43:21 +08:00
<?php
/**
*
* AI Labs extension
*
* @copyright (c) 2023, privet.fun, https://privet.fun
* @license GNU General Public License, version 2 (GPL-2.0)
*
*/
namespace privet\ailabs\includes;
class GenericCurl
{
2023-06-02 01:35:15 +08:00
private $headers;
private $contentTypes;
2023-05-28 09:43:21 +08:00
private int $timeout = 0;
private object $stream_method;
private string $proxy = "";
2023-06-02 01:35:15 +08:00
private $curlInfo = [];
2023-05-28 09:43:21 +08:00
public int $retryCount;
public int $timeoutBeforeRetrySec;
2023-06-02 01:35:15 +08:00
public $responseCodes = [];
2023-05-28 09:43:21 +08:00
public function __construct($API_KEY, $retryCount = 3, $timeoutBeforeRetrySec = 10)
{
$this->contentTypes = [
"application/json" => "Content-Type: application/json",
"multipart/form-data" => "Content-Type: multipart/form-data",
];
$this->headers = [
$this->contentTypes["application/json"],
"Authorization: Bearer $API_KEY",
];
$this->retryCount = $retryCount;
$this->timeoutBeforeRetrySec = $timeoutBeforeRetrySec;
}
/**
* @return array
* Remove this method from your code before deploying
*/
public function getCURLInfo()
{
return $this->curlInfo;
}
/**
* @param int $timeout
*/
public function setTimeout(int $timeout)
{
$this->timeout = $timeout;
}
/**
* @param string $proxy
*/
public function setProxy(string $proxy)
{
if ($proxy && strpos($proxy, '://') === false) {
$proxy = 'https://' . $proxy;
}
$this->proxy = $proxy;
}
/**
* @param array $header
* @return void
*/
2023-06-02 01:35:15 +08:00
public function setHeader($header)
2023-05-28 09:43:21 +08:00
{
if ($header) {
foreach ($header as $key => $value) {
$this->headers[$key] = $value;
}
}
}
/**
* @param string $url
* @param string $method
* @param array $opts
* @return bool|string
*/
2023-06-02 01:35:15 +08:00
public function sendRequest(string $url, string $method, $opts = [])
2023-05-28 09:43:21 +08:00
{
$this->responseCodes = [];
$post_fields = json_encode($opts);
if (array_key_exists('file', $opts) || array_key_exists('image', $opts)) {
$this->headers[0] = $this->contentTypes["multipart/form-data"];
$post_fields = $opts;
} else {
$this->headers[0] = $this->contentTypes["application/json"];
}
$curl_info = [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => $this->timeout,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_POSTFIELDS => $post_fields,
CURLOPT_HTTPHEADER => $this->headers,
];
if ($opts == []) {
unset($curl_info[CURLOPT_POSTFIELDS]);
}
if (!empty($this->proxy)) {
$curl_info[CURLOPT_PROXY] = $this->proxy;
}
if (array_key_exists('stream', $opts) && $opts['stream']) {
$curl_info[CURLOPT_WRITEFUNCTION] = $this->stream_method;
}
$curl = curl_init();
curl_setopt_array($curl, $curl_info);
$retryCount = 0;
$responseCode = 0;
$response = null;
do {
$retryCount++;
$response = curl_exec($curl);
$responseCode = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
array_push($this->responseCodes, $responseCode);
} while (
$responseCode !== 200 &&
$retryCount < $this->retryCount &&
sleep($this->timeoutBeforeRetrySec) !== false
);
$this->curlInfo = curl_getinfo($curl);
curl_close($curl);
return $response;
}
}