php获得远程信息到本地使用的3个函数:file_get_contents和curl函数和stream_get_contents

1:file_get_contents

echo file_get_contents("http://www.php.com/index.php");

 

2:curl

function getFile($url,$save_dir='',$filename='',$type=0){
  if(trim($url)==''){
   return false;
  }
  if(trim($save_dir)==''){
   $save_dir='./';
  }
  if(0!==strrpos($save_dir,'/')){
   $save_dir.='/';
  }
  //创建保存目录
  if(!file_exists($save_dir)&&!mkdir($save_dir,0777,true)){
   return false;
  }
 //获取远程文件所采用的方法
 if($type){
  $ch=curl_init();
  $timeout=5;
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
  $content=curl_exec($ch);
  curl_close($ch);
 }else{
  ob_start();
  readfile($url);
  $content=ob_get_contents();
  ob_end_clean();
}
 $size=strlen($content);
 //文件大小
 $fp2=@fopen($save_dir.$filename,'a');
 fwrite($fp2,$content);
 fclose($fp2);
 unset($content,$url);
 return array('file_name'=>$filename,'save_path'=>$save_dir.$filename);
}
 
getFile($url,$save_dir,$filename,1)//调用

3:stream_get_contents

$readcontents=fopen("http://www.php.com/index.php","rb");

$contents=stream_get_contents($readcontents);//这个函数有两个参数stream_get_contents($readcontents,-1,-1)

fclose($readcontents);

原文地址:https://www.cnblogs.com/hmetoer2017/p/6248182.html