我需要获取一些包含瑞典字母的URL。如果以https://en.wikipedia.org/wiki/Åland_Islands为例,直接将其作为参数传递给file_get_contents函数是可以正常工作的。但是,如果先通过urlencode对该URL进行编码,那么调用将会失败,出现以下信息:
failed to open stream: No such file or directory
尽管file_get_contents函数的文档说明如下:
注意:如果你打开一个包含特殊字符(比如空格)的URI,你需要使用urlencode()对其进行编码。
因此,例如运行以下代码:
failed to open stream: No such file or directory
尽管file_get_contents函数的文档说明如下:
注意:如果你打开一个包含特殊字符(比如空格)的URI,你需要使用urlencode()对其进行编码。
因此,例如运行以下代码:
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”,那么它将正常运行。
我面临的问题是,我的URL中有一个参数来自提交的表单。由于PHP始终通过urlencode
处理提交的值,因此我构建的URL中的瑞典字符将导致出现错误。
我该如何解决这个问题?