MortenMoulder
MortenMoulder

Reputation: 6648

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

Whenever I try to grab a page's content using file_get_contents(), and the domain has an unicode character in it, I get this:

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

This only happens when I have an unicode character in the domain. Here's an example:

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

Upvotes: 7

Views: 1984

Answers (2)

cmorrissey
cmorrissey

Reputation: 8583

You need to use the idn_to_ascii() function:

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

Reference:

Upvotes: 5

Felippe Duarte
Felippe Duarte

Reputation: 15131

You can use Punycode, which encode/decode IDNA names:

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

echo file_get_contents($url);

Upvotes: 2

Related Questions