在URL包含“Ö”字符的这种特殊情况下,我没有得到file_get_contents()返回页面。
$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()在此网址上正常工作?
我尝试了以下解决方案,但均未取得有效结果:
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));
发现以下问题:
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
更新:
如您所见,在我的示例中实际上返回了一个页面,但它不是预期的页面,即您在浏览器中键入url时获得的页面。
参考方案
网址不能包含“Ö”!从这个基本前提开始。不在狭窄定义的ASCII子集中的任何字符都必须经过URL编码才能在URL中表示。正确的方法是urlencode
或rawurlencode
(取决于服务器期望的格式)URL的各个部分,而不是整个URL。
例如。:
$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
。哪一个是正确的取决于服务器的期望。
我只是将一个项目从localhost移到了我的远程服务器上,并且注意到我的某些脚本停止工作了。最重要的是依赖file_get_contents()从另一个脚本中获取JSON值的脚本。PHP版本是5.2.4allow_url_fopen开启 警告:file_get_contents() [function.file-get-contents]:php_netw…
vfsstream:file_get_contents()无法打开流:stream_open调用失败 - php我已经设置了vfsstream块设备,并且尝试在其上调用file_get_contents()。但是,对vfsStreamWrapper::stream_open的调用失败,因此无法打开流。这是我的代码:$this->root = vfsStream::setup('root'); $this->root->addChi…
AJAX和file_get_contents - php我有一个使用file_get_contents的脚本,然后解码返回的JSON ...$urlone = json_decode(file_get_contents("http://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=f…
通过代理通过https请求获取file_get_contents - php如何通过代理服务器执行HTTPS请求代理服务器在debian上为tinyproxy码$context = stream_context_create([ 'http' => [ 'proxy' => 'tcp://xx.xx.xx.xx:8888', 'request_full…
使用try catch块无法捕获PHP file_get_contents错误 - php我正在尝试使用file_get_contents函数获取图像,但它给出了错误。为了处理错误,我使用了try catch块,但是它没有捕获到错误并且失败了。我的代码:try { $url = 'http://wxdex.ocm/pdd.jpg'; //dummy url $file_content = file_get_contents($…