时时商务社区

标题: php获取远程文件内容的函数 [打印本页]

作者: qz234    时间: 2018-2-14 05:41

            一个简单的php获取远程文件内容的函数代码,兼容性强。直接调用就可以轻松获取远程文件的内容,使用这个函数也可获取图片。代码如下:
/**
* 读远程内容
* @return string
*/
function get_url_content($url){
  if(function_exists("curl_init")){
    $ch = curl_init();
    $timeout = 30;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $file_contents = curl_exec($ch);
    curl_close($ch);
  }else{
    $is_auf=ini_get('allow_url_fopen')?true:false;
    if($is_auf){
      $file_contents = file_get_contents($url);
    }
  }
  return $file_contents;
}
以上就是php获取远程文件内容的函数代码,希望这篇文章对大家学习php程序设计有所帮助。
            
            
您可能感兴趣的文章:
  • php file_put_contents()功能函数(集成了fopen、fwrite、fclose)
  • PHP中创建空文件的代码[file_put_contents vs touch]
  • PHP中fwrite与file_put_contents性能测试代码
  • PHP文件锁定写入实例解析
  • php实现读取和写入tab分割的文件
  • php写入、删除与复制文件的方法
  • php逐行读取txt文件写入数组的方法
  • PHP使用缓存即时输出内容(output buffering)的方法
  • PHP使用内置函数file_put_contents写入文件及追加内容的方法
            




    欢迎光临 时时商务社区 (http://bbs.4435.cn/) Powered by Discuz! X3.2