duancongjue9202 2015-07-30 09:48
浏览 236
已采纳

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

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

Found in these questions:

file_get_contents - special characters in URL

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

file_get_contents() Breaks Up UTF-8 Characters

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.

  • 写回答

2条回答 默认 最新

  • doupai8095 2015-07-30 10:17
    关注

    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.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度