file_get_contents - Special characters in URL - Special case

与世无争的帅哥 提交于 2019-11-27 16:21:26

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.

E.g.:

$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'));

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.

One needs to percentage encode the unicode characters. This is one way that I know of doing it.

$url2 = "https://se.timeedit.net/web/liu/db1/schema/s/s.html?tab=3&object=" . urlencode('CM_949A11_1534_1603_DAG_DST_50_ÖVRIGT_1_1') . "&type=subgroup&startdate=20150101&enddate=20300501";
echo "encoded: " . $url2;
print file_get_contents($url2);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!