IpInfoServer.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2018/6/11
  6. * Time: 上午9:28
  7. */
  8. namespace common\api;
  9. use Yii;
  10. class IpInfoServer {
  11. /*
  12. * 传入IP地址,返回IP所在地信息
  13. * */
  14. public function getIpInfo($ip)
  15. {
  16. $data = json_decode(@file_get_contents('http://ip.taobao.com/service/getIpInfo.php?ip='.$ip),true);
  17. if(!empty($data) && $data['code'] == 0)
  18. {
  19. return $data['data'];
  20. }
  21. return false;
  22. }
  23. public function getUserIp()
  24. {
  25. return Yii::$app->request->userIP;
  26. }
  27. public static function LimitIp()
  28. {
  29. $ip =Yii::$app->request->userIP;
  30. $curl = new \linslin\yii2\curl\Curl();
  31. $iphttp = 'http://ossimg.niufangw.com/public/ipadopt?ip='.$ip.'&type=1';
  32. $ipResult = $curl->get($iphttp);
  33. if(!empty($ipResult))
  34. {
  35. $iprow = json_decode($ipResult,true);
  36. }
  37. if(!empty($ip) && (isset($iprow) && $iprow['code'] == 300))
  38. {
  39. $http = 'http://ip.taobao.com/service/getIpInfo.php?ip='.$ip;
  40. $result = $curl->get($http);
  41. if(!empty($result))
  42. {
  43. $res = json_decode($result,true);
  44. if(!empty($res) && $res['code'] == 0 && strpos($res['data']['region'],'海南') !== false)
  45. {
  46. header('Location:http://www.baidu.com');
  47. exit ;
  48. }
  49. if(empty($res))
  50. {
  51. header('Location:http://www.baidu.com');
  52. exit ;
  53. }
  54. $inputHttp = 'http://ossimg.niufangw.com/public/ipadd?ip='.$ip.'&type=1';
  55. $curl->get($inputHttp);
  56. }
  57. }
  58. }
  59. }