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

cURL defaults #10509

Closed
xepozz opened this issue Feb 5, 2023 · 12 comments
Closed

cURL defaults #10509

xepozz opened this issue Feb 5, 2023 · 12 comments

Comments

@xepozz
Copy link

xepozz commented Feb 5, 2023

Description

The main problem is cURL doesn't have any default settings and an ability to set them.
So there is no way to control all your outgoing requests. Some reasons to control them globally: proxying, removing some unacceptable headers, encoding, etc.
fopen/file_get_contents/file are great to use shared configuration, but:

  • They are not fluent compare to cURL methods
  • You cannot control vendor implementations uses cURL

The following code:

<?php
stream_context_set_default([
    'http' => [
        'proxy' => 'http://default-proxy',
    ],
]);
// Works
file_get_contents('https://www.google.com');
// Doesn't work
$ch = curl_init("https://www.google.com/");
curl_exec($ch);

I understand there are two different things, but global settings maybe incredible useful.
Due to cURL doesn't use streams, it would be great to have at least an additional method to set defaults.

So questions:

  1. Is there any problems to connect cURL with streams as a stream wrapper?
  2. Are there any cons not to share streams context with cURL options?
  3. Is it possible to add a method to set defaults?

PHP Version

8.2

Operating System

No response

@KapitanOczywisty
Copy link

  1. Is there any problems to connect cURL with streams as a stream wrapper?
  2. Are there any cons not to share streams context with cURL options?

Besides being hard to map stream context options to corresponding curl ones this would introduce backward compatibility issues.

3. Is it possible to add a method to set defaults?

Such function would be nice, but it has to go through RFC process Also related: https://wiki.php.net/rfc/curl_user_agent

Currently you can keep default options in one handle and clone it instead of calling curl_init:

$defaultCh = curl_init();
curl_setopt($defaultCh, CURLOPT_PROXY, 'http://192.168.0.1:8080');

$ch = clone $defaultCh;
curl_setopt($ch, CURLOPT_URL, 'https://example.com');
curl_exec();

@xepozz
Copy link
Author

xepozz commented Feb 5, 2023

Currently you can keep default options in one handle and clone it instead of calling curl_init:

Sure, but it won't work for vendor packages.

@blar
Copy link
Contributor

blar commented Feb 5, 2023

You can set the environment variables http_proxy or HTTPS_PROXY to use a global proxy for all curl requests.

@iluuu1994
Copy link
Member

@adoy Your insight would also be helpful here.

@adoy
Copy link
Member

adoy commented Mar 23, 2023

I have the same lecture and agree with @KapitanOczywisty. We shouldn't use the stream_context_set_default, curl is not a stream (experimental curl stream context was deleted in PHP 5.5). I think a new curl_* function would be the best. If someone want to write an RFC for it, I can take care of the actual implementation.

@github-actions
Copy link

There has not been any recent activity in this feature request. It will automatically be closed in 14 days if no further action is taken. Please see https://github.com/probot/stale#is-closing-stale-issues-really-a-good-idea to understand why we auto-close stale feature requests.

@github-actions github-actions bot added the Stale label Jun 22, 2023
@xepozz
Copy link
Author

xepozz commented Jun 22, 2023

Up

@github-actions github-actions bot removed the Stale label Jun 23, 2023
@github-actions
Copy link

There has not been any recent activity in this feature request. It will automatically be closed in 14 days if no further action is taken. Please see https://github.com/probot/stale#is-closing-stale-issues-really-a-good-idea to understand why we auto-close stale feature requests.

@github-actions github-actions bot added the Stale label Sep 22, 2023
@xepozz
Copy link
Author

xepozz commented Sep 22, 2023

Still want to have

@KapitanOczywisty
Copy link

@xepozz There is no point in keeping this issue alive, since you need to follow RFC process to make such changes. Are you willing to do that?

@xepozz
Copy link
Author

xepozz commented Sep 22, 2023

@KapitanOczywisty yes I'd like to do it, but can't actually try. Give me a few weeks, I'll try to follow the process

@github-actions github-actions bot removed the Stale label Sep 23, 2023
Copy link

There has not been any recent activity in this feature request. It will automatically be closed in 14 days if no further action is taken. Please see https://github.com/probot/stale#is-closing-stale-issues-really-a-good-idea to understand why we auto-close stale feature requests.

@github-actions github-actions bot added the Stale label Dec 22, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jan 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants