无法打开流:HTTP请求失败! HTTP / 1.1 400错误请求

时间:2010-12-27 14:10:11

标签: http httpwebrequest

我正在访问其他网站的图片。我收到此错误:

  

“无法打开流:HTTP请求失败!HTTP / 1.1 400错误请求”复制“一些(不是全部)”图像时出错。这是我的代码。

$img=$_GET['img']; //another website url
$file=$img;

function getFileextension($file) {
       return end(explode(".", $file));
}
$fileext=getFileextension($file);
if($fileext=='jpg' || $fileext=='gif' || $fileext=='jpeg' || $fileext=='png' || $fileext=='x-png' || $fileext=='pjpeg'){
if($img!=''){
$rand_variable1=rand(10000,100000);
              $node_online_name1=$rand_variable1."image.".$fileext;

                $s=copy($img,"images/".$node_online_name1);

}

6 个答案:

答案 0 :(得分:14)

我认为preg_replace更有意义,因为它可以与最新版本的PHP一起使用,因为ereg_replace不能让我被弃用

$url = preg_replace("/ /", "%20", $url);

答案 1 :(得分:11)

我有同样的问题,但它是由

解决的
$url = str_replace(" ", "%20", $url);

感谢Cello_Guy的帖子。

答案 2 :(得分:5)

我能想到的唯一问题是空间位于网址中,很可能是文件名中的空格。需要将URL中的所有空格转换为正确的编码,即%20。

如果你有这样的文件名:

http://www.somewhere.com/images/img 1.jpg”

你会得到上述错误,但有了这个:

http://www.somewhere.com/images/img%201.jpg

你应该遇到问题。

只需使用str_replace()替换空格(“”)进行正确编码(“%20”)

看起来像这样:

$url = str_replace(" ", "%20", $url);

有关str_replace()结帐The PHP Manual的更多信息。

答案 3 :(得分:1)

使用函数rawurlencode()

根据»RFC 3986对给定字符串进行编码。

http://php.net/manual/ru/function.rawurlencode.php

答案 4 :(得分:1)

即使url中的尾随空白也可能导致php文件($ url)失败。在最新版本的php或apache中,即使url中的尾随空白也会导致错误。因此,网址似乎在浏览器中工作,因为浏览器足够了解尾随空白或忽略它。无论如何,那是我的错误。

旧的LAMP允许它。 (即相同的代码运行正常)。轻松修复。

答案 5 :(得分:0)

URL似乎有一些空格或其他特殊字符,您需要对其进行编码,并使用urlencode()函数