ip.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. // 指定允许其他域名访问
  3. header('Access-Control-Allow-Origin:*');
  4. // 响应类型
  5. header('Access-Control-Allow-Methods:POST');
  6. // 响应头设置
  7. header('Access-Control-Allow-Headers:x-requested-with,content-type');
  8. /**
  9. * ajax业务处理中的接口转发层,解决ajax跨域访问的问题
  10. * 工作原理:问请求通过本程序做中转,在本地服务器层完成与远程服务接口的交互
  11. * 备注:使用时 URL_ROOT 这个参数需要根据你的目标接口地址进行修改,本转发层之能用于单接口的Web Service接口服务
  12. * 程序支持POST数据与GET数量的同时转发;
  13. * @version 1.0.0.2
  14. * @author JerryLi lijian@dzs.mobi
  15. * @copyright b.dzs.mobi 2012-11-16
  16. * */
  17. class interface_relay
  18. {
  19. /**接口根地址(此处是需要修改的地方)*/
  20. // const URL_ROOT = 'http://gd.geobytes.com/GetCityDetails?ie=utf-8';
  21. const URL_ROOT = 'http://pv.sohu.com/cityjson?ie=utf-8';
  22. // const URL_ROOT = 'http://hs.1473.cn/php/index.php?mode=outside.1473.cn,US_HospitalSystem,HS_HT_SelectDepartment,,10,1';
  23. /**字符集*/
  24. const CHARSET = 'UTF-8';
  25. /**GET*/
  26. private $msGets = '';
  27. /**POST*/
  28. private $maGetPostData = array();
  29. function __construct()
  30. {
  31. $this->getPOST();
  32. $this->getGET();
  33. if($this->msGets != '' || count($this->maGetPostData) > 0)
  34. { //存在输入数据
  35. // if(count($this->msGets) > 0)
  36. // $sUrl = self::URL_ROOT .'?'. $this->msGets;
  37. // else
  38. $sUrl = self::URL_ROOT;
  39. header('Content-Type: text/html; charset='. self::CHARSET);
  40. echo $this->getContent($sUrl);
  41. }
  42. else
  43. {
  44. header('Content-Type: text/html; charset='. self::CHARSET);
  45. echo $this->getContent(self::URL_ROOT);
  46. }
  47. }
  48. function __destruct()
  49. {
  50. unset($maGetPostData, $msGets);
  51. }
  52. /**
  53. * 载入POST数据
  54. * @return bool
  55. * */
  56. private function getPOST()
  57. {
  58. $handle = @fopen('php://input', 'r');
  59. $data = '';
  60. do
  61. {
  62. $data = @fread($handle, 1024);
  63. if (strlen($data) == 0)
  64. break;
  65. else
  66. $this->maGetPostData[] = $data;
  67. }while(true);
  68. fclose($handle);
  69. unset($data, $handle);
  70. return count($this->maGetPostData) >= 1;
  71. }
  72. /**
  73. * 载入GET数据
  74. * @return bool
  75. * */
  76. private function getGET()
  77. {
  78. /*取得GET内容*/
  79. if (count($_GET) > 0)
  80. {
  81. $aTmp = array();
  82. foreach ($_GET as $sKey => $sVal)
  83. $aTmp[] = $sKey .'='. urlencode($sVal);
  84. $this->msGets = implode('&', $aTmp);
  85. return true;
  86. }
  87. else
  88. return false;
  89. }
  90. /**
  91. * 读取远程接口返回的内容
  92. * @return string
  93. * */
  94. private function getContent($sGetUrl)
  95. {
  96. /**/
  97. $ch = curl_init();
  98. curl_setopt ($ch, CURLOPT_URL, $sGetUrl); //设置GET的URL地址
  99. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);//将结果保存成字符串
  100. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 10);//连接超时时间s
  101. curl_setopt ($ch, CURLOPT_TIMEOUT, 10);//执行超时时间s
  102. curl_setopt ($ch, CURLOPT_DNS_CACHE_TIMEOUT, 1800);//DNS解析缓存保存时间半小时
  103. curl_setopt($ch, CURLOPT_HEADER,0);//丢掉头信息
  104. if (count($this->maGetPostData) > 0)
  105. { //存在POST数据需要提交
  106. curl_setopt($ch, CURLOPT_POST, 1); //启用POST数据
  107. curl_setopt($ch, CURLOPT_POSTFIELDS, implode('', $this->maGetPostData));//提交POST数据
  108. }
  109. $sData = curl_exec($ch);
  110. curl_close($ch);
  111. unset($ch);
  112. return $sData;
  113. }
  114. }
  115. $o = new interface_relay();
  116. unset($o);
  117. ?>