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/");
If you're opening a URI with special characters, such as spaces, you need to encode the URI with urlencode () . Name of the file to read.
This extension reveals certain Unicode characters that easily corrupted because they are invisible or look like other normal characters. In PowerShell 6+, the default encoding is UTF-8 without BOM on all platforms. In Windows PowerShell, the default encoding is usually Windows-1252, an extension of latin-1, also known as ISO 8859-1.
On failure, file_get_contents () will return false . file_get_contents () is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance. If you're opening a URI with special characters, such as spaces, you need to encode the URI with urlencode () .
There is no way for PowerShell to automatically determine the file encoding. You're more likely to have encoding problems when you're using characters not in the 7-bit ASCII character set. For example:
You need to use the idn_to_ascii()
function:
file_get_contents('http://' . idn_to_ascii('møller.dk'));
Reference:
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With