File_get_contents()和域中的unicode字符(如æøå)


file_get_contents() and unicode characters in domain (like æøå)

每当我尝试使用file_get_contents()抓取页面内容时,并且域中有一个unicode字符,我得到这个:

file_get_contents(https://møller.dk/): failed to open stream: php_network_getaddresses: getaddrinfo failed: Name of service not known in>FILE LOCATION<</p>

这只发生在我在域中有一个unicode字符时。下面是一个例子:

file_get_contents("http://møller.dk/");

您需要使用idn_to_ascii()函数:

file_get_contents('http://' . idn_to_ascii('møller.dk'));
参考:

  • http://php.net/manual/en/function.idn-to-ascii.php

你可以使用Punycode,它可以编码/解码IDNA名称:

$Punycode = new Punycode();
$baseUrl = 'ærlig.no';
$url = 'http://'.$Punycode->encode($baseUrl);
echo file_get_contents($url);