Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [CURLRequest] add option for Proxy #7632

Merged
merged 2 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions system/HTTP/CURLRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,12 @@ protected function setCURLOptions(array $curlOptions = [], array $config = [])
}
}

// Proxy
if (isset($config['proxy'])) {
$curlOptions[CURLOPT_HTTPPROXYTUNNEL] = true;
$curlOptions[CURLOPT_PROXY] = $config['proxy'];
}

// Debug
if ($config['debug']) {
$curlOptions[CURLOPT_VERBOSE] = 1;
Expand Down
14 changes: 14 additions & 0 deletions tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,20 @@ public function testSSLWithBadKey()
]);
}

public function testProxyuOption()
{
$this->request->request('get', 'http://example.com', [
'proxy' => 'http://localhost:3128',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think proxy should be an array that can set CURLOPT_PROXY, CURLOPT_PROXYTYPE , CURLOPT_PROXYPORT and etc.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems Guzzle does not have such options.
https://docs.guzzlephp.org/en/stable/request-options.html#proxy

]);

$options = $this->request->curl_options;

$this->assertArrayHasKey(CURLOPT_PROXY, $options);
$this->assertSame('http://localhost:3128', $options[CURLOPT_PROXY]);
$this->assertArrayHasKey(CURLOPT_HTTPPROXYTUNNEL, $options);
$this->assertTrue($options[CURLOPT_HTTPPROXYTUNNEL]);
}

public function testDebugOptionTrue()
{
$this->request->request('get', 'http://example.com', [
Expand Down
14 changes: 14 additions & 0 deletions tests/system/HTTP/CURLRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,20 @@ public function testSSLWithBadKey()
]);
}

public function testProxyuOption()
{
$this->request->request('get', 'http://example.com', [
'proxy' => 'http://localhost:3128',
]);

$options = $this->request->curl_options;

$this->assertArrayHasKey(CURLOPT_PROXY, $options);
$this->assertSame('http://localhost:3128', $options[CURLOPT_PROXY]);
$this->assertArrayHasKey(CURLOPT_HTTPPROXYTUNNEL, $options);
$this->assertTrue($options[CURLOPT_HTTPPROXYTUNNEL]);
}

public function testDebugOptionTrue()
{
$this->request->request('get', 'http://example.com', [
Expand Down
3 changes: 2 additions & 1 deletion user_guide_src/source/changelogs/v4.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ Libraries
- **Validation:** Added ``Validation::getValidated()`` method that gets
the actual validated data. See :ref:`validation-getting-validated-data` for details.
- **Images:** The option ``$quality`` can now be used to compress WebP images.

- **Uploaded Files:** Added ``UploadedFiles::getClientPath()`` method that returns
the value of the `full_path` index of the file if it was uploaded via directory upload.
- **CURLRequest:** Added a request option ``proxy``. See
:ref:`CURLRequest Class <curlrequest-request-options-proxy>`.

Helpers and Functions
=====================
Expand Down
11 changes: 11 additions & 0 deletions user_guide_src/source/libraries/curlrequest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,17 @@ has been disabled. Any files that you want to send must be passed as instances o
``form_params`` for ``application/x-www-form-urlencoded`` requests, and ``multipart`` for ``multipart/form-data``
requests.

.. _curlrequest-request-options-proxy:

proxy
=====

.. versionadded:: 4.4.0

You can set a proxy by passing an associative array as the ``proxy`` option:

.. literalinclude:: curlrequest/035.php

query
=====

Expand Down
7 changes: 7 additions & 0 deletions user_guide_src/source/libraries/curlrequest/035.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

$client->request(
'GET',
'http://example.com',
['proxy' => 'http://localhost:3128']
);