【问题标题】:How to use file_get_contents() with non-English symbols in URL?如何在 URL 中使用带有非英文符号的 file_get_contents()?
【发布时间】:2013-01-03 21:12:14
【问题描述】:

当我尝试使用 PHP 的 file_get_contents() 函数访问非英语 (Unicode) URL 时出现此错误。网址是:http://ml.wikipedia.org/wiki/%E0%B4%B2%E0%B4%AF%E0%B4%A3%E0%B5%BD_%E0%B4%AE%E0%B5%86%E0%B4%B8%E0%B5%8D%E0%B4%B8%E0%B4%BF

我遇到了这个错误:

警告:file_get_contents(http://ml.wikipedia.org/wiki/%E0%B4%B2%E0%B4%AF%E0%B4%A3%E0%B5%BD_%E0%B4%AE% E0%B5%86%E0%B4%B8%E0%B5%8D%E0%B4%B8%E0%B4%BF) [function.file-get-contents]:无法打开流:HTTP 请求失败! HTTP/1.0 403 禁止..

致命错误:在第 8 行对 G:\xampp\htdocs\codes\htmlParse1.php 中的非对象调用成员函数 find()

file_get_contents() 函数有什么限制吗?它只接受英文网址吗?

【问题讨论】:

  • @viakondratiuk 你想说什么?上面的 URL 已经进行了 URL 编码。奇怪... URL 在浏览器中工作,我看不出它不应该的原因。它适用于纯 URL 吗?
  • @Pekka:是的,ascii-URL 可以正常工作……很奇怪
  • 那是什么语言?我的浏览器甚至无法正确显示这些字符...
  • @Calle:正如简森所说,没关系 - 斯洛伐克语也不起作用。试试 sk.wikipedia.org/wiki/Žuvačka
  • 我可以获得 sv.wikipedia.org 或 en.wikipedia.org 而无需对用户代理进行任何更改(如下面的解决方案所示)。我只是在扔东西:sv.wikipedia.org 有效,en.wikipedia.org 有效,但 ml.wikipedia.org 无效?为什么?好吧,ml.wikipedia.org 重定向到一个不是 URL 编码的 URL(至少在浏览器中没有这样显示),这样的重定向会导致问题吗?

标签: php url unicode file-get-contents


【解决方案1】:

您缺少诸如用户代理之类的标头信息。我建议你使用 Just use curl

$url = 'http://ml.wikipedia.org/wiki/%E0%B4%B2%E0%B4%AF%E0%B4%A3%E0%B5%BD_%E0%B4%AE%E0%B5%86%E0%B4%B8%E0%B5%8D%E0%B4%B8%E0%B4%BF';
$ch = curl_init($url); // initialize curl handle
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17");
curl_setopt($ch, CURLOPT_REFERER, "http://ml.wikipedia.org");
curl_setopt($ch, CURLOPT_ENCODING, "UTF-8");
$data = curl_exec($ch);
print($data);

Live CURL Demo

如果必须使用file_get_content

$options = array(
        'http'=>array(
                'method'=>"GET",
                'header'=>"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" .
                "Cookie: centralnotice_bucket=0-4.2; clicktracking-session=M7EcNiC2Zcuko7exVGUvLfdwxzSK3Boap; narayam-scheme=ml\r\n" . 
                "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17"
        )
);


$url = 'http://ml.wikipedia.org/wiki/%E0%B4%B2%E0%B4%AF%E0%B4%A3%E0%B5%BD_%E0%B4%AE%E0%B5%86%E0%B4%B8%E0%B5%8D%E0%B4%B8%E0%B4%BF';
$context = stream_context_create($options);
$file = file_get_contents($url, false, $context);
echo $file ;

Live file_get_content Demo

【讨论】:

    【解决方案2】:

    如果有403 Forbidden,连接应该可以工作。 这只是一个警告,网络服务器以状态码 403 响应。维基百科拒绝在没有有效用户代理的情况下进行下载:

    脚本应使用包含联系信息的用户代理字符串,否则它们可能会在不通知的情况下被 IP 阻止。

    第二个错误应该来自处理file_get_contents(...) 调用的结果(字符串对象)的下一行。

    编辑:您应该尝试设置您的用户代理,例如ini_set('user_agent', 'wikiPHP'); 在发出请求之前。这应该可以正常工作。

    【讨论】:

    • 不,只是在我自己的PHP服务器上检查过,根本没有数据发送到维基百科服务器。
    • 用用户代理试过了,仍然没有解决这个印度语言..:(
    • 我尝试过使用 Baba 的代码。工作!谢谢@baba,FurloSK,nlsbshtr。干杯..:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-07
    • 2011-02-25
    • 1970-01-01
    • 2019-02-15
    • 1970-01-01
    相关资源
    最近更新 更多