gpt4 book ai didi

PHP CURL(7.48.0) DNS解析在/etc/hosts

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:22:48 25 4
gpt4 key购买 nike

我遇到了一个关于 php curl 的奇怪问题。

它会连接到 DNS 服务器进行解析,即使主机在/etc/hosts 中!

软件版本:

    Server :    CentOS release 6.3 (Final)
    Linux Curl version :    curl --version    curl 7.47.1 (x86_64-pc-linux-gnu) libcurl/7.47.1 OpenSSL/1.0.1e zlib/1.2.3   libidn/1.18    Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3    pop3s rtsp smb smbs smtp smtps telnet tftp    Features: IDN IPv6 Largefile NTLM NTLM_WB SSL libz UnixSockets`
    PHP version :    php -v    PHP 5.4.41 (cli) (built: Aug 11 2015 22:29:40)    Copyright (c) 1997-2014 The PHP Group    Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
    PHP curl version :     php -r "var_export(curl_version());"    array (    'version_number' => 471040,    'age' => 3,    'features' => 952221,    'ssl_version_number' => 0,    'version' => '7.48.0',    'host' => 'x86_64-redhat-linux-gnu',    'ssl_version' => 'OpenSSL/1.0.1e',    'libz_version' => '1.2.3',

服务器配置:

    /etc/hosts :     127.0.0.1 lalala.examplekkk.com
    /etc/resolv.conf :     nameserver 8.8.8.8
    /etc/nsswitch.conf    hosts:      files dns

命令执行结果:

    PING lalala.examplekkk.com (127.0.0.1) 56(84) bytes of data.    64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.041 ms    64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.035 ms    ....
    strace php -r '$curl=curl_init();$url="http://lalala.examplekkk.com";curl_setopt($curl,CURLOPT_URL,$url);$content=curl_exec($curl);echo $content;'    ...    open("/etc/hosts", O_RDONLY)            = 3    fstat(3, {st_mode=S_IFREG|0644, st_size=1308, ...}) = 0    mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f85c7b5c000    read(3, "127.0.0.1   localhost localhost."..., 4096) = 1308    ...    fcntl(3, F_SETFD, FD_CLOEXEC)           = 0    connect(3, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("8.8.8.8")}, 16) = 0    sendto(3, "\177{\1\0\0\1\0\0\0\0\0\0\6lalala\nexamplekkk\3c"..., 39, MSG_NOSIGNAL, NULL, 0) = 39    poll([{fd=3, events=POLLIN|POLLRDNORM}], 1, 0) = 0 (Timeout)    ...

我很困惑为什么 php curl 需要连接到 dns 服务器来解析主机名??

我执行命令'strace curl 'http://lalala.examplekkk.com',不用连接8.8.8.8就可以了。

它是 php-curl 中的错误吗?

最佳答案

总结:CURLOPT_RESOLVE 是您问题的解决方案。

来自 https://curl.haxx.se/libcurl/c/CURLOPT_RESOLVE.html :

Provide custom host name to IP address resolves.

在 PHP 实现的情况下,您必须提供一个至少包含一个元素的数组。 cURL 仅查看数组值,而不查看键。

Each single name resolve string should be written using the format HOST:PORT:ADDRESS[,ADDRESS]... where HOST is the name libcurl will try to resolve, PORT is the port number of the service where libcurl wants to connect to the HOST and ADDRESS is one or more numerical IP addresses.

这是一个使用您的值的更完整的示例:

$curl = curl_init();
$options = [
CURLOPT_RESOLVE => ['lalala.examplekkk.com:80:127.0.0.1'],
CURLOPT_URL => "http://lalala.examplekkk.com/",
CURLOPT_RETURNTRANSFER => 1,
];
curl_setopt_array($curl, $options);
$content = curl_exec($curl);

这不是错误,而是有据可查的行为。

据我所知,您不能告诉 cURL 使用 /etc/hosts 文件。如果这不正确,请发表评论。

关于PHP CURL(7.48.0) DNS解析在/etc/hosts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40357176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com