From 75eea6849b0085e9d6e99a01eae8efadb8022cc5 Mon Sep 17 00:00:00 2001 From: alexchexes Date: Wed, 28 May 2025 17:39:38 +0100 Subject: [PATCH 1/2] feat: add support for proxy credentials --- README.md | 2 ++ src/OpenAi.php | 14 ++++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0f13e67..13adceb 100644 --- a/README.md +++ b/README.md @@ -343,6 +343,8 @@ You can use some proxy servers for your requests api; ````php $open_ai->setProxy("http://127.0.0.1:1086"); +// or with credentials +$open_ai->setProxy("http://127.0.0.1:1086", "myuser:mypassword"); ```` ## Set header diff --git a/src/OpenAi.php b/src/OpenAi.php index 87bfcbe..945379b 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -16,6 +16,7 @@ class OpenAi private object $stream_method; private string $customUrl = ""; private string $proxy = ""; + private ?string $proxyAuth = null; private array $curlInfo = []; public function __construct($OPENAI_API_KEY) @@ -880,14 +881,16 @@ public function setTimeout(int $timeout) } /** - * @param string $proxy + * @param string $proxy [scheme://]host[:port] + * @param string|null $proxyAuth user:password */ - public function setProxy(string $proxy) + public function setProxy(string $proxy, ?string $proxyAuth = null) { if ($proxy && strpos($proxy, '://') === false) { - $proxy = 'https://'.$proxy; + $proxy = 'http://'.$proxy; } $this->proxy = $proxy; + $this->proxyAuth = $proxyAuth; } /** @@ -991,8 +994,11 @@ private function sendRequest(string $url, string $method, array $opts = []) unset($curl_info[CURLOPT_POSTFIELDS]); } - if (! empty($this->proxy)) { + if ($this->proxy) { $curl_info[CURLOPT_PROXY] = $this->proxy; + if ($this->proxyAuth) { + $curl_info[CURLOPT_PROXYUSERPWD] = $this->proxyAuth; + } } if (array_key_exists('stream', $opts) && $opts['stream']) { From 609c6dcfa6e39a8ddd6270707f38bf3b7859a38d Mon Sep 17 00:00:00 2001 From: alexchexes Date: Wed, 28 May 2025 18:09:44 +0100 Subject: [PATCH 2/2] revert: unintended change of default proxy protocol from 'https' to 'http' --- src/OpenAi.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenAi.php b/src/OpenAi.php index 945379b..07bad66 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -887,7 +887,7 @@ public function setTimeout(int $timeout) public function setProxy(string $proxy, ?string $proxyAuth = null) { if ($proxy && strpos($proxy, '://') === false) { - $proxy = 'http://'.$proxy; + $proxy = 'https://'.$proxy; } $this->proxy = $proxy; $this->proxyAuth = $proxyAuth;