【问题标题】:failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request无法打开流:HTTP 请求失败! HTTP/1.1 400 错误请求
【发布时间】:2011-05-31 04:02:42
【问题描述】:

我正在访问另一个网站的图像。我收到此错误:

“无法打开流: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);

}

【问题讨论】:

  • 这是重现问题的所有代码吗?最后有一个未闭合的花括号。另外,您实际上是在哪里向 Web 服务器发送请求?当您收到错误时,请求是什么?
  • 我正在从另一个站点复制图像。当执行复制功能时,我会收到此错误。 $s=copy($img,"images/".$node_online_name1);

标签: http httpwebrequest


【解决方案1】:

我能想到的唯一问题是 url 中的空格,很可能在文件名中。 url 中的所有空格都需要转换为正确的编码,即 %20。

如果你有这样的文件名:

“http://www.somewhere.com/images/img1.jpg”

你会得到上面的错误,但是这样:

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

你应该有问题。

只需使用 str_replace() 替换空格 (" ") 以使其正确编码 ("%20")

看起来像这样:

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

有关str_replace() 的更多信息,请查看The PHP Manual。

【讨论】:

  • 比 str_replace 更好的是在“img 1”字符串上调用 urlencode
【解决方案2】:

我遇到了同样的问题,但是已经解决了

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

感谢 Cello_Guy 的帖子。

【讨论】:

  • 谢谢。你节省了我的时间
【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

    使用函数rawurlencode()

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

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

    【讨论】:

      【解决方案5】:

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

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

      【讨论】:

        【解决方案6】:

        URL好像有空格或者其他特殊字符,需要编码,使用urlencode()函数

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-12-18
          • 2017-08-07
          • 2012-12-31
          • 2015-04-10
          相关资源
          最近更新 更多