class.dev.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. defined('IN_WEB') or die('Include Error!');
  3. /**
  4. * 测试环境切换
  5. * @author LokChen
  6. * @date 2017/04/19
  7. */
  8. class LibDev {
  9. public static $cfg = array();
  10. public static function initCfg() {
  11. $file = WWWROOT . 'config/config.dev.php';
  12. if (file_exists($file)) {
  13. self::$cfg = include($file);
  14. }
  15. }
  16. public static function proxy() {
  17. self::initCfg();
  18. if (empty(self::$cfg)) {
  19. return false;
  20. }
  21. $devUsers = self::$cfg['conf'];
  22. $uid = (int)$_REQUEST['uid'];
  23. $ip = $_SERVER['REMOTE_ADDR'];
  24. $devName = '';
  25. if (isset(self::$cfg['uids'][$uid])) { // 如果配置UID
  26. $devName = self::$cfg['uids'][$uid];
  27. }
  28. if (isset(self::$cfg['ips'][$ip])) { // 如果有配置IP
  29. $devName = self::$cfg['ips'][$ip];
  30. }
  31. if (empty($devName)) {
  32. return false;
  33. }
  34. $ret = self::curl($devUsers[$devName]['api'], $devUsers[$devName]['host'], $_REQUEST);
  35. echo $ret;
  36. exit;
  37. }
  38. public static function curl($url, $host, $post_data = array()) {
  39. $timeout = 30;
  40. $ch = curl_init();
  41. curl_setopt($ch, CURLOPT_URL, $url);
  42. curl_setopt($ch, CURLOPT_HEADER, 0);
  43. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  44. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  45. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  46. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Host: {$host}"));
  47. if (!empty($post_data)) {
  48. curl_setopt($ch, CURLOPT_POST, true);
  49. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  50. }
  51. $result = curl_exec($ch);
  52. curl_close($ch);
  53. return $result;
  54. }
  55. public static function test() {
  56. return "test";
  57. }
  58. }