PHP file_get_contents和重音URL(4行代码示例)

I looked at other questions here and still cant solve this issue. It may look like a duplicate but the answers like adding urlencode of the URL does not work.

I'm trying to fetch images with file_get_contents. here is the code:

 $works='http://static.groupalia.it/DealImage/Local/Epilazione-definitiva-con-IFL/Epilazione-definitiva-con-IFL/be71fdb9-4f7c-4e8e-ab4f-4d9742fa1acd.JPG';
 $url='http://static.groupalia.it/DealImage/Local/Hostaria-cacio-e-cocci--menù-x2/Hastaria-Cacio-e-Cocci-Menu--x2/933d79e8-103d-4ffe-b84b-70cf87b14f96.JPG';
 $img= file_get_contents ($url);
 if ( strlen($img) >1000) echo "WORKS!";

I'm trying to find a solution that will work for both URLS (the $works and $url).

If I add urlencode to the url gives 'failed to open stream' on both $url and $works.

What is the right solution that works in all these cases?

You can try this:

<?php

$works  ='http://static.groupalia.it/DealImage/Local/Epilazione-definitiva-con-IFL/Epilazione-definitiva-con-IFL/be71fdb9-4f7c-4e8e-ab4f-4d9742fa1acd.JPG';
$url    ='http://static.groupalia.it/DealImage/Local/Hostaria-cacio-e-cocci--menù-x2/Hastaria-Cacio-e-Cocci-Menu--x2/933d79e8-103d-4ffe-b84b-70cf87b14f96.JPG';

$transliterator = Transliterator::create('Latin-ASCII');

$img = file_get_contents($transliterator->transliterate($works));

if (strlen($img) >1000) {
    echo "WORKS!";
}

But, imho, the best solutions is use e.g. https://github.com/Behat/Transliterator

Eventually this worked:

setlocale(LC_ALL, 'en_US.utf8'); // required for iconv
$url= iconv("UTF-8", "ASCII//TRANSLIT", $url);

Hope it helps someone else.