file_get_contents()失败,URL中带有特殊字符 [英] file_get_contents() fails with special characters in URL
问题描述
我需要从瑞典字母中获取一些包含某些字符的URL.
I have a need to fetch some URL's which have some characters from the Swedish alphabet.
如果您使用诸如https://en.wikipedia.org/wiki/Åland_Islands
这样的字符串作为示例,则将其直接作为参数传递到file_get_contents
调用中就可以了.但是,如果您首先通过urlencode
运行该URL,则调用将失败,并显示以下消息:
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
尽管file_get_contents
的文档说:
注意:如果您要打开带有特殊字符(例如空格)的URI, 您需要使用urlencode()对URI进行编码.
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;
您将收到错误.如果您只是从代码中删除"urlencode",它将正常运行.
You will get the error. If you just remove the "urlencode" from the code, it will run just fine.
我面临的问题是我的URL中有一个参数是从提交的表单中获取的.而且由于PHP始终通过urlencode
运行提交的值,所以在我构造的URL中的瑞典语字符将导致错误发生.
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.
我该如何解决?
推荐答案
该问题很可能是由于urlencode逃避了您的协议:
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.
这是我的方法:
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
代码:
$url = 'https://en.wikipedia.org/wiki/Åland_Islands';
$encodedUrl = urlencode($url);
$fixedEncodedUrl = str_replace(['%2F', '%3A'], ['/', ':'], $encodedUrl);
希望有帮助.
这篇关于file_get_contents()失败,URL中带有特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!