thinkphp 抓取网站的内容并且保存到本地的实例详解

  public function download() {

  $http = new OrgNetHttp();

  $url_pref = "http://www.dzkbw.com/books/rjb/dili/xc7s/";

  $localUrl = "Public/bookcover/";

  $reg="|showImg('(.+)');|";

  $i=1;

  do {

  $filename = substr("000".$i,-3).".htm";

  $ch = curl_init();

  curl_setopt($ch, CURLOPT_URL, $url_pref.$filename);

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);

  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

  $html = curl_exec($ch);

  curl_close($ch);

  $result = preg_match_all($reg,$html,$out, PREG_PATTERN_ORDER);

  if($result==1) {

  $picUrl = $out[1][0];

  $picFilename = substr("000".$i,-3).".jpg";

  $http->curlDownload($picUrl, $localUrl.$picFilename);

  }

  $i = $i+1;

  } while ($result==1);

  echo "下载完成";

  }