PHP curl无法解析纯IPv6 URL [英] PHP curl failing to resolve IPv6-only URL

查看:441
本文介绍了PHP curl无法解析纯IPv6 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图从 http://ipv6.test-ipv6.com 获取数据,该数据仅具有AAAA IPv6记录(无IPv4)。我可以从 curl -v http://ipv6.test-ipv6.com 这样的命令行获取数据(表明IP已正确解析)。我还尝试了 dig AAAA ipv6.test-ipv6.com @ 127.0.0.1 ,它运行良好,并且可以在PHP curl调用中指定IP时获取数据: $ ch = curl_init('http:// [2001:470:1:18 :: 119]'); 。但是专门使用仅IPv6的主机名会失败。我该如何解决?

I'm trying to get data from http://ipv6.test-ipv6.com, which only has AAAA IPv6 record (no IPv4). I can get the data from command line like curl -v http://ipv6.test-ipv6.com (shows that the IP is resolved properly). I also tried dig AAAA ipv6.test-ipv6.com @127.0.0.1 and it works fine, as well as getting the data when I specify the IP in PHP curl call: $ch = curl_init('http://[2001:470:1:18::119]');. But specifically using the IPv6-only hostname fails. How do I fix this?

代码:

<?php

$ch = curl_init('http://ipv6.test-ipv6.com');
//$ch = curl_init('http://[2001:470:1:18::119]');
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_VERSION_IPV6);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_exec($ch);

实际上从外观上看,问题出在 CURLOPT_IPRESOLVE 选项-如果没有该选项,它将起作用。我想要实现的目的是使curl能够使用IPv6地址,或者在无法使用时失败。这不是 CURLOPT_IPRESOLVE 应该做什么?

Actually by the looks of it, the problem is with CURLOPT_IPRESOLVE options - it works without that option. What I want to achieve is to get curl to use IPv6 address or fail if it's unavailable. Isn't that what CURLOPT_IPRESOLVE should do?

推荐答案

您的期望 CURLOPT_IPRESOLVE 是正确的,这确实是应该做的。但是,您似乎为该值使用了错误的常数。从手册中:

Your expectations of CURLOPT_IPRESOLVE are correct, that is indeed what it is supposed to do. However you seem to have used a wrong constant for the value. From the manual:


CURLOPT_IPRESOLVE :允许应用程序选择要处理的IP
地址类型在解析主机名时使用。仅当使用主机名解析IP地址超过一个
版本的
时,这才是有趣的,可能的值为 CURL_IPRESOLVE_WHATEVER
CURL_IPRESOLVE_V4 >, CURL_IPRESOLVE_V6 ,默认为
CURL_IPRESOLVE_WHATEVER

CURLOPT_IPRESOLVE: Allows an application to select what kind of IP addresses to use when resolving host names. This is only interesting when using host names that resolve addresses using more than one version of IP, possible values are CURL_IPRESOLVE_WHATEVER, CURL_IPRESOLVE_V4, CURL_IPRESOLVE_V6, by default CURL_IPRESOLVE_WHATEVER.

I找不到手册中使用的常量( CURL_VERSION_IPV6 ),因此IIRC然后PHP将其解释为字符串 CURL_VERSION_IPV6 无效。

I can't find the constant you use (CURL_VERSION_IPV6) in the manual, so IIRC then PHP will interpret it as the string "CURL_VERSION_IPV6" which won't work.

这篇关于PHP curl无法解析纯IPv6 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆