想实现一种功能 当访问的时候
p.php?p=图片网址
需要使用CURL方式抓取 可否给个代码 谢谢
php 用CURL 抓取图片
答案:2 悬赏:40 手机版
解决时间 2021-02-05 11:31
- 提问者网友:杀生予夺
- 2021-02-04 15:29
最佳答案
- 五星知识达人网友:骨子里都是戏
- 2021-02-04 16:25
preg_match('@p\.php\?p=(.*)@Ui', $url, $url);//获取图片地址
if(isset($url[1]))
$url=$url[1];
else
$url='';
if($url):
//curl抓取图片过程
$ch = curl_init();
if (defined('CURLOPT_IPRESOLVE') && defined('CURL_IPRESOLVE_V4')) {
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$content = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if ($info['http_code'] != 200)
$content = NULL;
if($content)//保存图片到本地
@file_put_contents ('存放地址', $content);
endif;
if(isset($url[1]))
$url=$url[1];
else
$url='';
if($url):
//curl抓取图片过程
$ch = curl_init();
if (defined('CURLOPT_IPRESOLVE') && defined('CURL_IPRESOLVE_V4')) {
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$content = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if ($info['http_code'] != 200)
$content = NULL;
if($content)//保存图片到本地
@file_put_contents ('存放地址', $content);
endif;
全部回答
- 1楼网友:蓝房子
- 2021-02-04 17:24
淘宝采用的是https,看看你的php curl中是否有设置以下参数:
curl_setopt($ch, curlopt_ssl_verifypeer, false);
curl_setopt($ch, curlopt_ssl_verifyhost, false);
给一个完整的函数吧,抓淘宝亲测可用:
function request_url($url) {
$ch = curl_init();
curl_setopt($ch, curlopt_url, $url);
curl_setopt($ch, curlopt_failonerror, false);
curl_setopt($ch, curlopt_returntransfer, true);
//https 请求
if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
curl_setopt($ch, curlopt_ssl_verifypeer, false);
curl_setopt($ch, curlopt_ssl_verifyhost, false);
}
curl_setopt($ch, curlopt_referer, $url);
curl_setopt($ch, curlopt_encoding, 'gzip');
$reponse = curl_exec($ch);
return $reponse;
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯