test.php 3.4 KB

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