HttpUtils.class.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. include_once(__DIR__."/error.inc.php");
  3. class HttpUtils
  4. {
  5. //
  6. // public:
  7. //
  8. static public function MakeUrl($queryArgs)
  9. {
  10. $base = "https://qyapi.weixin.qq.com";
  11. if (substr($queryArgs, 0, 1) === "/")
  12. return $base . $queryArgs;
  13. return $base . "/" . $queryArgs;
  14. }
  15. static public function Array2Json($arr)
  16. {
  17. $parts = array ();
  18. $is_list = false;
  19. $keys = array_keys ( $arr );
  20. $max_length = count ( $arr ) - 1;
  21. if (($keys [0] === 0) && ($keys [$max_length] === $max_length )) {
  22. $is_list = true;
  23. for($i = 0; $i < count ( $keys ); $i ++) {
  24. if ($i != $keys [$i]) {
  25. $is_list = false;
  26. break;
  27. }
  28. }
  29. }
  30. foreach ( $arr as $key => $value ) {
  31. if (is_array ( $value )) {
  32. if ($is_list)
  33. $parts [] = self::array2Json ( $value );
  34. else
  35. $parts [] = '"' . $key . '":' . self::array2Json ( $value );
  36. } else {
  37. $str = '';
  38. if (! $is_list)
  39. $str = '"' . $key . '":';
  40. if (!is_string ( $value ) && is_numeric ( $value ) && $value<2000000000)
  41. $str .= $value;
  42. elseif ($value === false)
  43. $str .= 'false';
  44. elseif ($value === true)
  45. $str .= 'true';
  46. else
  47. $str .= '"' .addcslashes($value, "\\\"\n\r\t/"). '"';
  48. $parts[] = $str;
  49. }
  50. }
  51. $json = implode ( ',', $parts );
  52. if ($is_list)
  53. return '[' . $json . ']';
  54. return '{' . $json . '}';
  55. }
  56. /**
  57. * http get
  58. * @param string $url
  59. * @return http response body
  60. */
  61. static public function httpGet($url)
  62. {
  63. $config = require(__DIR__.'./../config.php');
  64. if (true == $config['DEBUG']) {
  65. echo $url . "\n";
  66. }
  67. self::__checkDeps();
  68. $ch = curl_init();
  69. self::__setSSLOpts($ch, $url);
  70. curl_setopt($ch, CURLOPT_URL, $url);
  71. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  72. return self::__exec($ch);
  73. }
  74. /**
  75. * http post
  76. * @param string $url
  77. * @param string or dict $postData
  78. * @return http response body
  79. */
  80. static public function httpPost($url, $postData)
  81. {
  82. $config = require(__DIR__.'./../config.php');
  83. if (true == $config['DEBUG']) {
  84. echo $url . " -d $postData\n";
  85. }
  86. self::__checkDeps();
  87. $ch = curl_init();
  88. self::__setSSLOpts($ch, $url);
  89. curl_setopt($ch, CURLOPT_URL, $url);
  90. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  91. curl_setopt($ch, CURLOPT_POST, true);
  92. curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  93. return self::__exec($ch);
  94. }
  95. //
  96. // private:
  97. //
  98. static private function __setSSLOpts($ch, $url)
  99. {
  100. if (stripos($url,"https://") !== false) {
  101. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  102. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  103. curl_setopt($ch, CURLOPT_SSLVERSION, 1);
  104. }
  105. }
  106. static private function __exec($ch)
  107. {
  108. $output = curl_exec($ch);
  109. $status = curl_getinfo($ch);
  110. curl_close($ch);
  111. if ($output === false) {
  112. throw new NetWorkError("network error");
  113. }
  114. if (intval($status["http_code"]) != 200) {
  115. throw new HttpError(
  116. "unexpected http code ". intval($status["http_code"]));
  117. }
  118. return $output;
  119. }
  120. static private function __checkDeps()
  121. {
  122. if (!function_exists("curl_init")) {
  123. throw new InternalError("missing curl extend");
  124. }
  125. }
  126. }