File_get_contents - special characters in the url

I am trying to get images from a remote server using file_get_contents . The image URL may contain spaces and / or special characters, such as Γ½, Γ‘, Γ­, etc., And when it performs an operation with HTTP code 400 - Bad Request. If I try to encode the url (or segments of this URL) using urlencode or rawurlencode , I get 404.

If the image URL does not contain spaces or special characters, it loads without problems.

I have a hunch that this has something to do with coding, but I just can't figure it out. Is there a coding method that I am missing? Is there a header that needs to be set for the request?

+6
source share
1 answer

The problem with file_get_contents is UTF-8 encoding (not yet implemented in PHP). If you want to upload a file using this function, you should do something like this in your URL:

$url_utf8 = rawurlencode(utf8_encode($url));

And after:

$content = file_get_contents($url_utf8);

+1
source

Source: https://habr.com/ru/post/914619/


All Articles