Forward.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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://www.uessq.com/region.php';
  21. // const URL_ROOT = 'http://hs.1473.cn/php/index.php?mode=outside.1473.cn,US_HospitalSystem,HS_HT_SelectDepartment,,10,1';
  22. /**字符集*/
  23. const CHARSET = 'UTF-8';
  24. /**GET*/
  25. private $msGets = '';
  26. /**POST*/
  27. private $maGetPostData = array();
  28. function __construct()
  29. {
  30. $this->getPOST();
  31. $this->getGET();
  32. if($this->msGets != '' || count($this->maGetPostData) > 0)
  33. { //存在输入数据
  34. // if(count($this->msGets) > 0)
  35. // $sUrl = self::URL_ROOT .'?'. $this->msGets;
  36. // else
  37. $sUrl = self::URL_ROOT;
  38. header('Content-Type: text/html; charset='. self::CHARSET);
  39. echo $this->getContent($sUrl);
  40. }
  41. else
  42. {
  43. header('Content-Type: text/html; charset='. self::CHARSET);
  44. echo $this->getContent(self::URL_ROOT);
  45. }
  46. }
  47. function __destruct()
  48. {
  49. unset($maGetPostData, $msGets);
  50. }
  51. /**
  52. * 载入POST数据
  53. * @return bool
  54. * */
  55. private function getPOST()
  56. {
  57. $handle = @fopen('php://input', 'r');
  58. $data = '';
  59. do
  60. {
  61. $data = @fread($handle, 1024);
  62. if (strlen($data) == 0)
  63. break;
  64. else
  65. $this->maGetPostData[] = $data;
  66. }while(true);
  67. fclose($handle);
  68. unset($data, $handle);
  69. return count($this->maGetPostData) >= 1;
  70. }
  71. /**
  72. * 载入GET数据
  73. * @return bool
  74. * */
  75. private function getGET()
  76. {
  77. /*取得GET内容*/
  78. if (count($_GET) > 0)
  79. {
  80. $aTmp = array();
  81. foreach ($_GET as $sKey => $sVal)
  82. $aTmp[] = $sKey .'='. urlencode($sVal);
  83. $this->msGets = implode('&', $aTmp);
  84. return true;
  85. }
  86. else
  87. return false;
  88. }
  89. /**
  90. * 读取远程接口返回的内容
  91. * @return string
  92. * */
  93. private function getContent($sGetUrl)
  94. {
  95. /**/
  96. $ch = curl_init();
  97. curl_setopt ($ch, CURLOPT_URL, $sGetUrl); //设置GET的URL地址
  98. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);//将结果保存成字符串
  99. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 10);//连接超时时间s
  100. curl_setopt ($ch, CURLOPT_TIMEOUT, 10);//执行超时时间s
  101. curl_setopt ($ch, CURLOPT_DNS_CACHE_TIMEOUT, 1800);//DNS解析缓存保存时间半小时
  102. curl_setopt($ch, CURLOPT_HEADER,0);//丢掉头信息
  103. if (count($this->maGetPostData) > 0)
  104. { //存在POST数据需要提交
  105. curl_setopt($ch, CURLOPT_POST, 1); //启用POST数据
  106. curl_setopt($ch, CURLOPT_POSTFIELDS, implode('', $this->maGetPostData));//提交POST数据
  107. }
  108. $sData = curl_exec($ch);
  109. curl_close($ch);
  110. unset($ch);
  111. return $sData;
  112. }
  113. }
  114. $o = new interface_relay();
  115. unset($o);
  116. ?>