<?php


if (!function_exists('read_file')) {
function ip() {
  $ip_long = array(
    array('607649792', '608174079'), // 36.56.0.0-36.63.255.255
    array('1038614528', '1039007743'), // 61.232.0.0-61.237.255.255
    array('1783627776', '1784676351'), // 106.80.0.0-106.95.255.255
    array('2035023872', '2035154943'), // 121.76.0.0-121.77.255.255
    array('2078801920', '2079064063'), // 123.232.0.0-123.235.255.255
    array('-1950089216', '-1948778497'), // 139.196.0.0-139.215.255.255
    array('-1425539072', '-1425014785'), // 171.8.0.0-171.15.255.255
    array('-1236271104', '-1235419137'), // 182.80.0.0-182.92.255.255
    array('-770113536', '-768606209'), // 210.25.0.0-210.47.255.255
    array('-569376768', '-564133889'), // 222.16.0.0-222.95.255.255
  );
  $rand_key = mt_rand(0, 9);
  return long2ip(mt_rand($ip_long[$rand_key][0], $ip_long[$rand_key][1]));
}
/*
function ip() {
  $ip1 = round(rand(600000, 2550000) / 10000);
  $ip2 = round(rand(600000, 2550000) / 10000);
  $ip3 = round(rand(600000, 2550000) / 10000);
  $ip4 = round(rand(600000, 2550000) / 10000);
  return ''.$ip1.'.'.$ip2.''.$ip3.'.'.$ip4.'';
}
*/
/*
  function getRealURL($url){
    stream_context_set_default(
      array(
        'http' => array(
            'method'  => 'GET',
            'timeout' => (float)0.5, // 发现这个超时时间有点偏大误差，更具实际选择吧
        )
      )
    );
    $header = get_headers($url, 1);
    if (strpos($header[0],'301') || strpos($header[0],'302')) {
      $header = array_change_key_case($header);
      if(is_array($header['location'])) {
        $url_ = $header['location'][count($header['location'])-1];
      }else{
        $url_ = $header['location'];
      }
      $url = stripos('A'.$url_, 'http') == 1 ? $url_ : rtrim($url, '/').'/'.ltrim($url_, '/');
    }
    return $url;
  }
*/
function read_file($url) {
  $timeout_ = 5; //设置一个超时时间，单位为秒
  if (function_exists('stream_context_set_default')) {
    stream_context_set_default(
      array(
        'http' => array(
          'method'  => 'GET',
          'timeout' => $timeout_,
        ),
        'ssl' => array(
          'verify_peer' => false,
          'verify_peer_name' => false,
        )
      )
    );
  } else {
    $ctx = stream_context_create(
      array(
        'http' => array(
          'method'  => 'GET',
          'timeout' => $timeout_,
        ),
        'ssl' => array(
          'verify_peer' => false,
          'verify_peer_name' => false,
        )
      )
    );
  }

  if (preg_match('/^https?\:\/\//i', $url, $http)) {
    $header = get_headers($url, 1);
    if (isset($header[0]) && preg_match('/\d{3}(\.\d+)?/', $header[0], $http_code)) {
      if ($http_code[0] >= 500) {
        return '';
      } elseif ($http_code[0] >= 400) {
        if ($http_code[0] != 412 && $http_code[0] != 403) {
          return '';
        }
      } elseif ($http_code[0] >= 300) {
        $header = array_change_key_case($header);
        if (isset($header['location'])) {
          if (is_array($header['location'])) {
            //$url_ = $header['location'][0];
            $url_ = $header['location'][count($header['location'])-1];
          } else {
            $url_ = $header['location'];
          }
          $url_ = preg_replace('/^\/\//', $http[0], $url_);
          $url = stripos('A'.$url_, 'http') == 1 ? $url_ : rtrim($url, '/').'/'.ltrim($url_, '/');
        }
      }/* else {
        return '';
      }*/
    }/* else {
      return '';
    }*/
	  
    $ip = ip();
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout_);           //设置超时   
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout_);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_URL, $url);                   //设置访问的url地址   
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);        //返回结果
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);        //跟踪301 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:'.$ip.'', 'CLIENT-IP:'.$ip.''));
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11");    //用户访问代理 User-Agent   
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);       //禁止 cURL 验证对等证书
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_SSLVERSION, FALSE);
    curl_setopt($ch, CURLOPT_REFERER, $url);               //设置 referer 
    $r = curl_exec($ch);
    curl_close($ch);
  }
  if (!$r) {
    $r = file_get_contents($url);
    if (!$r) {
      $r = file_get_contents($url, 0, $ctx);
    }
  }
  return $r;

}
}

?>