AipHttpUtil.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace common\helpers\ocr\baidu\lib;
  3. /**
  4. * BCE Util
  5. */
  6. class AipHttpUtil
  7. {
  8. // 根据RFC 3986,除了:
  9. // 1.大小写英文字符
  10. // 2.阿拉伯数字
  11. // 3.点'.'、波浪线'~'、减号'-'以及下划线'_'
  12. // 以外都要编码
  13. public static $PERCENT_ENCODED_STRINGS;
  14. //填充编码数组
  15. public static function __init()
  16. {
  17. AipHttpUtil::$PERCENT_ENCODED_STRINGS = array();
  18. for ($i = 0; $i < 256; ++$i) {
  19. AipHttpUtil::$PERCENT_ENCODED_STRINGS[$i] = sprintf("%%%02X", $i);
  20. }
  21. //a-z不编码
  22. foreach (range('a', 'z') as $ch) {
  23. AipHttpUtil::$PERCENT_ENCODED_STRINGS[ord($ch)] = $ch;
  24. }
  25. //A-Z不编码
  26. foreach (range('A', 'Z') as $ch) {
  27. AipHttpUtil::$PERCENT_ENCODED_STRINGS[ord($ch)] = $ch;
  28. }
  29. //0-9不编码
  30. foreach (range('0', '9') as $ch) {
  31. AipHttpUtil::$PERCENT_ENCODED_STRINGS[ord($ch)] = $ch;
  32. }
  33. //以下4个字符不编码
  34. AipHttpUtil::$PERCENT_ENCODED_STRINGS[ord('-')] = '-';
  35. AipHttpUtil::$PERCENT_ENCODED_STRINGS[ord('.')] = '.';
  36. AipHttpUtil::$PERCENT_ENCODED_STRINGS[ord('_')] = '_';
  37. AipHttpUtil::$PERCENT_ENCODED_STRINGS[ord('~')] = '~';
  38. }
  39. /**
  40. * 在uri编码中不能对'/'编码
  41. * @param string $path
  42. * @return string
  43. */
  44. public static function urlEncodeExceptSlash($path)
  45. {
  46. return str_replace("%2F", "/", AipHttpUtil::urlEncode($path));
  47. }
  48. /**
  49. * 使用编码数组编码
  50. * @param string $value
  51. * @return string
  52. */
  53. public static function urlEncode($value)
  54. {
  55. $result = '';
  56. for ($i = 0; $i < strlen($value); ++$i) {
  57. $result .= AipHttpUtil::$PERCENT_ENCODED_STRINGS[ord($value[$i])];
  58. }
  59. return $result;
  60. }
  61. /**
  62. * 生成标准化QueryString
  63. * @param array $parameters
  64. * @return array
  65. */
  66. public static function getCanonicalQueryString(array $parameters)
  67. {
  68. //没有参数,直接返回空串
  69. if (count($parameters) == 0) {
  70. return '';
  71. }
  72. $parameterStrings = array();
  73. foreach ($parameters as $k => $v) {
  74. //跳过Authorization字段
  75. if (strcasecmp('Authorization', $k) == 0) {
  76. continue;
  77. }
  78. if (!isset($k)) {
  79. throw new \InvalidArgumentException(
  80. "parameter key should not be null"
  81. );
  82. }
  83. if (isset($v)) {
  84. //对于有值的,编码后放在=号两边
  85. $parameterStrings[] = AipHttpUtil::urlEncode($k)
  86. . '=' . AipHttpUtil::urlEncode((string) $v);
  87. } else {
  88. //对于没有值的,只将key编码后放在=号的左边,右边留空
  89. $parameterStrings[] = AipHttpUtil::urlEncode($k) . '=';
  90. }
  91. }
  92. //按照字典序排序
  93. sort($parameterStrings);
  94. //使用'&'符号连接它们
  95. return implode('&', $parameterStrings);
  96. }
  97. /**
  98. * 生成标准化uri
  99. * @param string $path
  100. * @return string
  101. */
  102. public static function getCanonicalURIPath($path)
  103. {
  104. //空路径设置为'/'
  105. if (empty($path)) {
  106. return '/';
  107. } else {
  108. //所有的uri必须以'/'开头
  109. if ($path[0] == '/') {
  110. return AipHttpUtil::urlEncodeExceptSlash($path);
  111. } else {
  112. return '/' . AipHttpUtil::urlEncodeExceptSlash($path);
  113. }
  114. }
  115. }
  116. /**
  117. * 生成标准化http请求头串
  118. * @param array $headers
  119. * @return array
  120. */
  121. public static function getCanonicalHeaders($headers)
  122. {
  123. //如果没有headers,则返回空串
  124. if (count($headers) == 0) {
  125. return '';
  126. }
  127. $headerStrings = array();
  128. foreach ($headers as $k => $v) {
  129. //跳过key为null的
  130. if ($k === null) {
  131. continue;
  132. }
  133. //如果value为null,则赋值为空串
  134. if ($v === null) {
  135. $v = '';
  136. }
  137. //trim后再encode,之后使用':'号连接起来
  138. $headerStrings[] = AipHttpUtil::urlEncode(strtolower(trim($k))) . ':' . AipHttpUtil::urlEncode(trim($v));
  139. }
  140. //字典序排序
  141. sort($headerStrings);
  142. //用'\n'把它们连接起来
  143. return implode("\n", $headerStrings);
  144. }
  145. }