未能打开流:HTTP请求失败!HTTP / 1.1 400错误请求

时间:2022-05-23 01:51:37

I am accessing images from another website. I am getting "failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request " error when copying 'some(not all)' images. here is my code.

我正在访问另一个网站的图片。我将“失败于开放流: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);

}

}

5 个解决方案

#1


14  

I think preg_replace make more better sense as it will work with latest versions of the PHP as ereg_replace didn't worked for me being deprecated

我认为preg_replace更有意义,因为它将与最新版本的PHP一起工作,因为ereg_replace对我来说并不适用

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

#2


10  

I had the same problem, but it was solve by

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

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

Thanks Cello_Guy for the post.

谢谢Cello_Guy的留言。

#3


4  

The only issue I can think of is spaces being in the url, most likely in the file name. All spaces in a url need to be converted to their proper encoding, which is %20.

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

If you have a file name like this:

如果你有这样的文件名:

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

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

You would get the above error, but with this:

你会得到上面的错误,但是有了这个:

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

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

You should have to problems.

你应该有问题。

Just use the str_replace() to replace the spaces (" ") for their proper encoding ("%20")

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

It looks like this:

它看起来像这样:

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

For more information on the str_replace() check out The PHP Manual.

有关str_replace()的更多信息,请参阅PHP手册。

#4


1  

Use the function rawurlencode()

使用函数rawurlencode()

Encodes the given string according to » RFC 3986.

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

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

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

#5


1  

Even a trailing blank in the url can cause php file($url) to fail. In recent versions of php or apache even a trailing blank in the url will cause the error. So the url appears to work in a browser because the browser knows enough to %20 the trailing blank or ignore it. That was my error anyway.

即使url后面有一个空格也会导致php文件($url)失败。在php或apache的最新版本中,即使url后面的空白也会导致错误。因此,url在浏览器中似乎是有效的,因为浏览器知道足够的%20的尾空或忽略它。那是我的错误。

Older LAMP allowed it. (ie. same code ran ok). Easy fix.

老灯允许它。(即。相同的代码跑好了)。简单的修复。

#1


14  

I think preg_replace make more better sense as it will work with latest versions of the PHP as ereg_replace didn't worked for me being deprecated

我认为preg_replace更有意义,因为它将与最新版本的PHP一起工作,因为ereg_replace对我来说并不适用

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

#2


10  

I had the same problem, but it was solve by

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

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

Thanks Cello_Guy for the post.

谢谢Cello_Guy的留言。

#3


4  

The only issue I can think of is spaces being in the url, most likely in the file name. All spaces in a url need to be converted to their proper encoding, which is %20.

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

If you have a file name like this:

如果你有这样的文件名:

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

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

You would get the above error, but with this:

你会得到上面的错误,但是有了这个:

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

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

You should have to problems.

你应该有问题。

Just use the str_replace() to replace the spaces (" ") for their proper encoding ("%20")

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

It looks like this:

它看起来像这样:

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

For more information on the str_replace() check out The PHP Manual.

有关str_replace()的更多信息,请参阅PHP手册。

#4


1  

Use the function rawurlencode()

使用函数rawurlencode()

Encodes the given string according to » RFC 3986.

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

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

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

#5


1  

Even a trailing blank in the url can cause php file($url) to fail. In recent versions of php or apache even a trailing blank in the url will cause the error. So the url appears to work in a browser because the browser knows enough to %20 the trailing blank or ignore it. That was my error anyway.

即使url后面有一个空格也会导致php文件($url)失败。在php或apache的最新版本中,即使url后面的空白也会导致错误。因此,url在浏览器中似乎是有效的,因为浏览器知道足够的%20的尾空或忽略它。那是我的错误。

Older LAMP allowed it. (ie. same code ran ok). Easy fix.

老灯允许它。(即。相同的代码跑好了)。简单的修复。