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

时间:2016-10-30 14:50:43

标签: php curl web-scraping

我试图从网址下载图片,尝试了多种方法(我在谷歌上发现的几乎所有方法)但尚未成功, 有些解决方案会出错

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);

请帮帮我

1 个答案:

答案 0 :(得分:0)

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

$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);