>  기사  >  php教程  >  php读取远程服务文件

php读取远程服务文件

WBOY
WBOY원래의
2016-06-08 17:30:02899검색

下面来看看根据php读取远程服务文件

<script>ec(2);</script>

function get_content($url)
{
 if(!strpos($url, '://')) return 'Invalid URI';
 $content = '';
 if(ini_get('allow_url_fopen'))
 {
  $content = file_get_contents($url);
 }
 elseif(function_exists('curl_init'))
 {
  $handle = curl_init();
  curl_setopt($handle, CURLOPT_URL, $url);
  curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
  curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($handle, CURLOPT_FOLLOWLOCATION, 0);
  $content = curl_exec($handle);
  curl_close($handle);
 }
 elseif(function_exists('fsockopen'))
 {
  $urlinfo = parse_url($url);
  $host = $urlinfo['host'];
  $str = explode($host, $url);
  $uri = $str[1];
  unset($urlinfo, $str);
  $content = '';
  $fp = fsockopen($host, 80, $errno, $errstr, 30);
  if(!$fp)
  {
   $content = 'Can Not Open Socket...';
  }
  else
  {
   $out = "GET $uri   HTTP/1.1rn";
   $out.= "Host: $host rn";
   $out.= "Accept: */*rn";
   $out.= "User-Agent: $_SERVER[HTTP_USER_AGENT]rn";
   $out.= "Connection: Closernrn";
   fputs($fp, $out);
   while (!feof($fp))
   {
    $content .= fgets($fp, 4069);
   }
   fclose($fp);
  }
 }
 if(empty($content)) $content = 'Can Not Open Url, Please Check You Server ...
For More Information, Please Visit www.111cn.net;
 return $content;
}

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.