curl 抓取图片(PHP)

本文提供了一个使用PHP通过cURL抓取网络图片的示例代码。该示例展示了如何设置cURL选项来请求特定URL上的图片资源,并将其内容输出或保存到本地文件。文中还介绍了如何配置User-Agent和Referer等头部信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?
$p="http://news.cnhubei.com/hbrb/hbrbsglk/hbrb01/200905/W020090504254969587296.jpg";
$t_url ="http://news.cnhubei.com/hbrb/hbrbsglk/hbrb01/200905/t662541.shtml";

$rs = file_get_pic($p);
echo $rs;

function file_get_pic($str) {
global $url_domin,$t_url;
$ch = curl_init();
//echo " <BR>".$from_url;
curl_setopt($ch, CURLOPT_URL, $str); 
curl_setopt($ch, CURLOPT_HEADER, 0);            //返回内容中包含 HTTP 头
//curl_setopt($ch,CURLOPT_POST,1);

        $useragent="Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; QQDownload 1.7; TencentTraveler 4.0";
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);

curl_setopt($ch, CURLOPT_REFERER, $t_url);
curl_setopt($ch,CURLOPT_COOKIESESSION,true);
//curl_setopt($ch,CURLOPT_COOKIE,"hiido_lv=1241423490703&hiido_ti=1241425695046&hiido_tod=4&hiido_ui=0.14425078706025363");


curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
//echo $t_url;
$str = curl_exec($ch);                       
curl_close($ch);
return $str;
}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值