Home  >  Article  >  Backend Development  >  Example of using PHP to help curl capture remote URL images (solve the problem of anti-leeching)

Example of using PHP to help curl capture remote URL images (solve the problem of anti-leeching)

巴扎黑
巴扎黑Original
2016-11-09 11:33:061419browse

php uses curl to grab remote images, which can solve the problem of anti-leeching.

function ycimg($file,$newfile)
{
// 初始化一个 cURL 对象
$curl = curl_init(); 
// 设置你需要抓取的URL
curl_setopt($curl, CURLOPT_URL, $file);
// 设置header
curl_setopt($curl, CURLOPT_HEADER, 0);
// 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// 运行cURL,请求网页
$data = curl_exec($curl);
// 关闭URL请求
curl_close($curl);
//写入获得的数据
$write = @fopen($newfile,"w");
fwrite($write,$data);
fclose($write);
return TRUE;
}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn