无法使用php下载图像(错误的请求)


Could not download image using php (bad request)

我试图从一个url下载图像,尝试了多种方法(几乎每一个方法我在谷歌上找到),但还没有成功,一些解给出错误

Found Document has been moved here

和大多数解给出错误

file_get_content: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request

这是我迄今为止尝试过的解决方案之一(我已经尝试了超过25种)

$fp = fopen ('mydirectory/image.jpg', 'w+');              // open file handle
$ch = curl_init('https://www.otakusmash.com/read-manga/mangas/AIKI/008/Aiki_v02_c08_000.jpg');
            // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // enable if you want
curl_setopt($ch, CURLOPT_FILE, $fp);          // output to file
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1000);      // some large value to allow curl to run for a long time
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
curl_setopt($ch, CURLOPT_VERBOSE, true);   // Enable this line to see debug prints
curl_exec($ch);
curl_close($ch);                              // closing curl handle
fclose($fp);

请帮我一下

您的代码有错误,请使用下面的代码

$fp = fopen ("mydirectory/image.jpg", 'w+');              // open file handle
$ch = curl_init('https://www.otakusmash.com/read-manga/mangas/AIKI/008/Aiki_v02_c08_000.jpg');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // enable if you want
curl_setopt($ch, CURLOPT_FILE, $fp);          // output to file
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1000);      // some large value to allow curl to run for a long time
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
curl_setopt($ch, CURLOPT_VERBOSE, true);   // Enable this line to see debug prints
curl_exec($ch);
curl_close($ch);                              // closing curl handle
fclose($fp);