On 27/06/2025 04:51, Eric Norris wrote:
I can try to take a look at the curl options and do some github
searches to see if I can identify common patterns. I agree that
setting the HTTP method and timeout are good contenders. If someone
else wants to propose a list as well, feel free!
For what it's worth, I grepped my work code base for "CURLOPT_", and grouped the results into the categories below. Some of these are definitely more "advanced" than others, and a lot could just be one helper covering the whole category.
Really basic (already in the proposed constructor):
CURLOPT_URL
Setting HTTP method:
CURLOPT_HTTPGET
CURLOPT_POST
CURLOPT_CUSTOMREQUEST
Request body (despite name, handles both form data and raw string bodies):
CURLOPT_POSTFIELDS
Request headers:
CURLOPT_HTTPHEADER
CURLOPT_USERAGENT
CURLOPT_ENCODING (deprecated in favour of CURLOPT_ACCEPT_ENCODING)
Response body handling:
CURLOPT_FILE
CURLOPT_RETURNTRANSFER
Response header handling (extremely fiddly; it would be AMAZING to have something like getLastResponseHeaders() returning an array):
CURLOPT_HEADER
CURLOPT_HEADERFUNCTION
Auth:
CURLOPT_HTTPAUTH
CURLOPT_USERPWD
CURLOPT_SSLCERT (there's also a CURLOPT_SSLCERT_BLOB)
CURLOPT_SSLKEY
Error and redirect handling:
CURLOPT_FOLLOWLOCATION
CURLOPT_FAILONERROR
Timeouts:
CURLOPT_CONNECTTIMEOUT
CURLOPT_TIMEOUT
CURLOPT_TIMEOUT_MS
Cookie handling:
CURLOPT_COOKIE
CURLOPT_COOKIEFILE
CURLOPT_COOKIEJAR
Connection parameter tuning:
CURLOPT_HTTP_VERSION
CURLOPT_SSLVERSION
CURLOPT_SSL_VERIFYPEER (common, but shouldn't be!)
Debug output (like response headers, this is very unintuitive for PHP users; $ch->enableVerboseLogs() and $ch->getVerboseLogs() would be very handy):
CURLOPT_VERBOSE
CURLOPT_CAINFO
CURLOPT_STDERR
If we were to go this route, I would suggest:
public function fetch(): string
public function execute(resource|callable $out): void
that is, one method to just return the contents of the URL, and
another to either write output to a file (including STDOUT, if the
user desires), or to send output to a write callback.
That makes a lot of sense! I've never been very good at keeping names and sentences short :)
--
Rowan Tommins
[IMSoP]