您的位置:首页 > 编程语言 > PHP开发

PHP 抓取html页面内容 函数

2014-11-12 18:40 309 查看
function getHtml($url,$id=0){
  $file_path = ATTACHEMENT_PATH.'caiji/';
  if ($id) {
    $file_path.=($id%10).'/';
  }
  if (!is_dir($file_path)) {
    mkdir($file_path,0777,true);
  }
  $fname = md5(trim($url));
  $file = $file_path.$fname.'.txt';
  $string = file_get_contents($file);
  if (!$string) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 50);
    $string = curl_exec($ch);
    file_put_contents($file, $string);
  }
  return $string ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: