Urlencode和file_get_contents [英] Urlencode and file_get_contents

查看:86
本文介绍了Urlencode和file_get_contents的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在$string

我正在尝试做的事情(是的,URL周围有一个空格):

What I'm trying to do (yes, there is a whitespace around the url):

$string = urlencode(trim($string));
$string_data = file_get_contents($string);

我得到的(@也被替换):

What I get (@ is also replaced):

file_get_contents(http%3A%2F%2Fsite.s3.amazonaws.com%2Fimages%2Fsome+image+@name.jpg)[function.file-get-contents]: failed to open stream: No such file or directory

如果将http://site.s3.amazonaws.com/images/some image @name.jpg复制/粘贴到浏览器地址栏中,则图像将打开.

If you copy/paste http://site.s3.amazonaws.com/images/some image @name.jpg into browser address bar, image will open.

有什么不好的地方以及如何解决这个问题?

What's bad and how to fix that?

推荐答案

您指定的URL无效. file_get_contents需要一个有效的http URI(更确切地说,底层的http包装器需要).由于您的无效URI无效,因此file_get_contents失败.

The URL you have specified is invalid. file_get_contents expects a valid http URI (more precisely, the underlying http wrapper does). As your invalid URI is not a valid URI, file_get_contents fails.

您可以通过将无效的URI转换为有效URI来解决此问题. RFC3986 中提供了有关如何编写有效URI的信息.您需要注意所有特殊字符都正确表示.例如空格到加号之间,并且必须在广告位处对URL进行编码.同样,也需要删除开头和结尾的多余空白.

You can fix this by turning your invalid URI into a valid URI. Information how to write a valid URI is available in RFC3986. You need to take care that all special characters are represented correctly. e.g. spaces to plus-signs, and the commercial at sign has to be URL encoded. Also superfluous whitespace at beginning and end need to be removed.

完成后,网络服务器将告诉您禁止访问.然后,您可能需要通过HTTP上下文选项为HTTP文件包装添加其他请求标头,以解决此问题.您可以在PHP手册中找到相关信息: http://-https://—访问HTTP网址

When done, the webserver will tell you that the access is forbidden. You then might need to add additional request headers via HTTP context options for the HTTP file wrapper to solve that. You find the information in the PHP manual: http:// -- https:// — Accessing HTTP(s) URLs

这篇关于Urlencode和file_get_contents的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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