file_get_contents - URL中的特殊字符 - 特殊情况 [英] file_get_contents - Special characters in URL - Special case

查看:863
本文介绍了file_get_contents - URL中的特殊字符 - 特殊情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此特定情况下,我不会获取file_get_contents()返回页面,其中url包含Ö字符。

I'm not getting file_get_contents() to return the page in this particular case where the url contains an 'Ö' character.

$url = "https://se.timeedit.net/web/liu/db1/schema/s/s.html?tab=3&object=CM_949A11_1534_1603_DAG_DST_50_ÖVRIGT_1_1&type=subgroup&startdate=20150101&enddate=20300501"
print file_get_contents($url);

如何使file_get_contents()在此网址上正常工作?

How do I make file_get_contents() work as expected on this url?

我尝试过以下解决方案,但没有工作结果:

I have tried following solutions whithout a working result:

1。

print rawurlencode(utf8_encode($url));

2。

print mb_convert_encoding($url, 'HTML-ENTITIES', "UTF-8");

3。

$url = urlencode($url);
print file_get_contents($url);

4。

$content = file_get_contents($url);
print mb_convert_encoding($content, 'UTF-8', mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true));

找到以下问题:

a href =http://stackoverflow.com/questions/10396777/file-get-contents-special-characters-in-url> file_get_contents - 网址中的特殊字符

file_get_contents - special characters in URL

PHP get url with special characters without urlencode :ing it!

file_get_contents()分隔UTF-8字符

UPDATE:
正如你所看到的,在我的示例中,但它不是预期的页面,当您在浏览器中键入的URL。

UPDATE: As you can see a page is actually returned in my example but it is not the expected page, the one you get when you type the url in the browser.

推荐答案

网址不能包含Ö!从这个基本前提开始。不在狭义定义的ASCII子集中的任何字符必须进行URL编码才能在URL中表示。正确的做法是 urlencode rawurlencode (取决于服务器期望的格式)个别段网址不是整个网址

URLs cannot contain "Ö"! Start from this basic premise. Any characters not within a narrowly defined subset of ASCII must be URL-encoded to be represented within a URL. The right way to do that is to urlencode or rawurlencode (depending on which format the server expects) the individual segment of the URL, not the URL as a whole.

例如:

$url = sprintf('https://se.timeedit.net/web/liu/db1/schema/s/s.html?tab=3&object=%s&type=subgroup&startdate=20150101&enddate=20300501',
               rawurlencode('CM_949A11_1534_1603_DAG_DST_50_ÖVRIGT_1_1'));

您仍然需要为字符串使用正确的编码! ISO-8859-1中的Ö将URL编码为%D6 ,而在UTF-8中,到%C3%96 。哪一个是正确的取决于服务器的期望。

You will still need to use the correct encoding for the string! Ö in ISO-8859-1 would be URL encoded to %D6, while in UTF-8 it would be encoded to %C3%96. Which one is the correct one depends on what the server expects.

这篇关于file_get_contents - URL中的特殊字符 - 特殊情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆