加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

php 抓取图片

发布时间:2020-05-25 07:02:32 所属栏目:PHP 来源:互联网
导读:php 抓取图片

下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。

脚本之家小编现在分享给大家,也给大家做个参考。

php 获取网站图片并存入本地文件
<?php
/**
 * Created by PhpStorm.
 * Date: 15-2-9
 * Time: 下午3:10
 */
 
for($i=0;$i<1000;$i++){
    $getData['page'] =  $i+1;
    getImg($getData);
}
 
 
function getImg($getData)
{
    $path = "D:/NFS/img/";
    $opts = array(
        'http'=>array(
            'method'=>"GET",'timeout'=>3,//设置3秒等待
        ),);
    if(is_array($getData) && !empty($getData))
    {
        $ser = http_build_query($getData,'','&');
    }
    $cnt=0;
   //如果请求失败 继续请求3次
    while($cnt<3 && ($file=file_get_contents("http://sexy.faceks.com/?{$ser}",false,stream_context_create($opts)))===FALSE) $cnt++;
    preg_match_all("/<img.*?src="(.*?)"[^>]*>/i",$file,$matches);
    if(empty($matches[0]) || empty($matches[1]))
    {
        echo "not img...";die;
    }
 
    $imgs=array_unique($matches[1]);
    foreach($imgs as $k=>$img)
    {
 
        $filename = $path.time().$k.".jpg";
        getImage($img,$filename);
    }
 
}
 
/**
 * @param string $url
 * @param string $fileName
 * 保存图片
 */
function getImage($url = '',$fileName = '')
{
    var_dump($fileName);
    $ch = curl_init();
    $fp = fopen($fileName,'wb');
 
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_FILE,$fp);
    curl_setopt($ch,CURLOPT_HEADER,0);
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ch,CURLOPT_TIMEOUT,60);
 
    curl_exec($ch);
    curl_close($ch);
    fclose($fp);
}

以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读