无法使用PHP CURL下载远程图像

问题描述 投票:0回答:1

[我正在尝试使用CURL和php下载远程图像。下面是我的代码。但是,每当我尝试打开下载的图像时,总会得到:

Cannot read this file. This is not a valid bitmap file, or its format is not currently supported.

任何人都知道我的代码有什么问题吗?谢谢。

$image ="http://api.scrapestack.com/scrape?access_key=TOKEN-HERE&url=https://imgur.com/a/E5ehGuv";
$imageName = pathinfo( $image, PATHINFO_BASENAME );
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $image );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
$source = curl_exec( $ch );
$info = curl_getinfo($ch);
curl_close( $ch );
file_put_contents( $imageName, $source ));
php curl php-curl
1个回答
0
投票

您的问题是此URL。

$image ="http://api.scrapestack.com/scrape?access_key=TOKEN-HERE&url=https://imgur.com/a/E5ehGuv";

如果您转到此网址

https://imgur.com/a/E5ehGuv

您将看到图像页面,但NOT图像路径。 pathinfo()功能在这里不起作用,并引发错误。

如果右键单击该图像,然后在新选项卡中打开图像,您将看到图像路径,在这种情况下为

https://i.imgur.com/Cbiu8Ef.png

所以您可以尝试使用此URL

$image ="http://api.scrapestack.com/scrape?access_key=TOKEN-HERE&url=https://i.imgur.com/Cbiu8Ef.png";
© www.soinside.com 2019 - 2024. All rights reserved.