如何从卷曲中获取图像 [英] how to get image from curl

查看:69
本文介绍了如何从卷曲中获取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了代码中的URL,我的脚本运行良好。我总是得到404。当然我可以从浏览器中获取图像。有人可以帮我解决这个问题吗?
PS:实际上,我可以使用int方法 questions / 6476212来获取图像文件。但是,当尝试打开图像文件时,文件中的内容是404页面而不是图像。

My script works well except for the url in the code. I always get a 404. Of course I can get the image from browser. Could anybody help me fix this? PS:Actually, I could use method int "questions/6476212" to get the image file. But when trying to open the image file, the content in the file is a 404 page not the image.

$url = 'https://spthumbnails.5min.com/10368406/518420256_c_570_411.jpg';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.1 Safari/537.11');
$res = curl_exec($ch);
$rescode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
curl_close($ch) ;
echo $res;"


推荐答案

您的脚本是正确编写的。我已经尝试过并且工作正常,但是您需要设置图片标题才能使导航器显示图片。

Your script is correctly written. I have tried and works fine, but you need to set image headers in order to let navigator show the image.

示例:

header("Content-Type: image/jpeg");

$url = 'https://images.nga.gov/en/web_images/constable.jpg';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.1 Safari/537.11');
$res = curl_exec($ch);
$rescode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
curl_close($ch) ;
echo $res;

但是请注意您的图像网址已关闭,这就是为什么您会收到404错误。

But please note your image url is down, that's why you are getting 404 error.

Rega rds

这篇关于如何从卷曲中获取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆