Skip to content

curl_setopt_array() not properly honoured when using CURLOPT_DNS_SERVERS #17610

@Muffinman

Description

@Muffinman

Description

The following code:

<?php
$options = [
    \CURLOPT_DNS_SERVERS => '8.8.8.8',
    \CURLOPT_CUSTOMREQUEST => 'GET',
    \CURLOPT_URL => 'http://example.com/',
    \CURLOPT_RETURNTRANSFER => false,
    \CURLE_ABORTED_BY_CALLBACK => false,
    \CURLOPT_CONNECTTIMEOUT => 300,
    \CURLOPT_PROTOCOLS => 3,
    \CURLOPT_HTTP_VERSION => 2,
    \CURLOPT_SSL_VERIFYHOST => 2,
    \CURLOPT_SSL_VERIFYPEER => true,
    \CURLOPT_ACCEPT_ENCODING => "",
    \CURLOPT_HTTPHEADER => [
        'Accept-Encoding:',
        'User-Agent: GuzzleHttp/7',
        'Host: example.com',
        'Accept:',
    ],
    \CURLOPT_WRITEFUNCTION => static function ($ch, $write) {
        return strlen($write);
    },
    \CURLOPT_TIMEOUT_MS => 2000.0,
    \CURLOPT_STDERR => fopen('php://stdout', 'w'),
    \CURLOPT_VERBOSE => true,
    \CURLOPT_HEADERFUNCTION => static function ($ch, $header) {
        echo $header;
        return strlen($header);
    },
];

$ch = curl_init();
curl_setopt_array($ch, $options);
curl_exec($ch);

if (curl_error($ch)) {
    echo curl_error($ch);
}
curl_close($ch);

Resulted in this output:

No URL set 

But I expected this output instead:

* Host example.com:80 was resolved.
* IPv6: (none)
* IPv4: 23.215.0.138, 23.192.228.84, 96.7.128.198, 23.215.0.136, 96.7.128.175, 23.192.228.80
*   Trying 23.215.0.138:80...
* Connected to example.com (23.215.0.138) port 80
* using HTTP/1.x
> GET / HTTP/1.1
Host: example.com
User-Agent: GuzzleHttp/7

* Request completely sent off
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Content-Type: text/html
Content-Type: text/html
< ETag: "84238dfc8092e5d9c0dac8ef93371a07:1736799080.121134"
ETag: "84238dfc8092e5d9c0dac8ef93371a07:1736799080.121134"
< Last-Modified: Mon, 13 Jan 2025 20:11:20 GMT
Last-Modified: Mon, 13 Jan 2025 20:11:20 GMT
< Cache-Control: max-age=2337
Cache-Control: max-age=2337
< Date: Tue, 28 Jan 2025 11:46:44 GMT
Date: Tue, 28 Jan 2025 11:46:44 GMT
< Content-Length: 1256
Content-Length: 1256
< Connection: keep-alive
Connection: keep-alive
<

* Connection #0 to host example.com left intact

When CURLOPT_DNS_SERVERS is used in the curl_setopt_array() array, all subsequent array keys seem to be ignored or otherwise discarded.

If used in curl_setopt() it works as expected:

// Doing it this way works
curl_setopt_array($ch, $options);
curl_setopt($ch, CURLOPT_DNS_SERVERS, '8.8.8.8');
curl_exec($ch);

Ref: guzzle/guzzle#3265

PHP Version

PHP 8.3.15

Operating System

MacOS 15.2

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions