dtw52353 2015-06-28 08:48
浏览 343
已采纳

file_get_contents()失败,URL中包含特殊字符

I have a need to fetch some URL's which have some characters from the Swedish alphabet.

If you take an example of such string as https://en.wikipedia.org/wiki/Åland_Islands, passing that straight into the file_get_contents call as a parameter works just fine. But if you run that URL through urlencode first, then the call fails with the message:

failed to open stream: No such file or directory

despite the documentation for file_get_contents saying:

Note: If you're opening a URI with special characters, such as spaces, you need to encode the URI with urlencode().

So for example, if you run the following code:

error_reporting(E_ALL);
ini_set("display_errors", true);

$url = urlencode("https://en.wikipedia.org/wiki/Åland_Islands");

$response = file_get_contents($url);
if($response === false) {
    die('file get contents has failed');
}
echo $response;

You will get the error. If you just remove the "urlencode" from the code, it will run just fine.

The problem I am facing is that there is a parameter in my URL that is taken from a submitted form. And since PHP always runs submitted values through the urlencode, the Swedish characters in my constructed URL will cause the error to happen.

How do I get around this?

  • 写回答

2条回答 默认 最新

  • ds08541 2015-06-28 09:23
    关注

    The problem is likely due to urlencode escaping your protocol:

    https://en.wikipedia.org/wiki/Åland_Islands
    https%3A%2F%2Fen.wikipedia.org%2Fwiki%2F%C3%85land_Islands
    

    This is a problem I have also faced, and could only fix by trying to target the escaping to only what is necessary for escape:

    https://en.wikipedia.org/wiki/Åland_Islands
    https://en.wikipedia.org/wiki/%C3%85land_Islands    
    

    This is as can be imagined tricky depending on where your characters are located. I usually opt for an encode patch solution, but some people I have worked with prefer to only encode the dynamic segment of their urls.

    Here is my approach:

    https://en.wikipedia.org/wiki/Åland_Islands
    https%3A%2F%2Fen.wikipedia.org%2Fwiki%2F%C3%85land_Islands
    https://en.wikipedia.org/wiki/%C3%85land_Islands
    

    Code:

    $url = 'https://en.wikipedia.org/wiki/Åland_Islands';
    $encodedUrl = urlencode($url);
    $fixedEncodedUrl = str_replace(['%2F', '%3A'], ['/', ':'], $encodedUrl);
    

    Hope it helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么