WebUtils.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. include_once 'IcbcConstants.php';
  3. class WebUtils{
  4. private static $version = "v2_20170324";
  5. public static function doGet($url, $params, $charset){
  6. $headers = array();
  7. $headers[IcbcConstants::$VERSION_HEADER_NAME] = self::$version;
  8. $getUrl = self::buildGetUrl($url, $params, $charset);
  9. $ch = curl_init();
  10. curl_setopt($ch, CURLOPT_URL, $getUrl);
  11. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  12. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  13. curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
  14. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS,8000);
  15. curl_setopt($ch, CURLOPT_TIMEOUT_MS, 30000);
  16. $response = curl_exec($ch);
  17. $resinfo = curl_getinfo($ch);
  18. curl_close($ch);
  19. if($resinfo["http_code"] != 200){
  20. throw new Exception("response status code is not valid. status code: ".$resinfo["http_code"]);
  21. }
  22. return $response;
  23. }
  24. public static function doPost($url, $params, $charset){
  25. $headers = array();
  26. $headers[] = 'Expect:';
  27. $headers[IcbcConstants::$VERSION_HEADER_NAME] = self::$version;
  28. $ch = curl_init();
  29. curl_setopt($ch, CURLOPT_URL, $url);
  30. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  31. curl_setopt($ch, CURLOPT_POST, true);
  32. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
  33. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  34. curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
  35. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS,8000);
  36. curl_setopt($ch, CURLOPT_TIMEOUT_MS, 30000);
  37. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
  38. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
  39. $response = curl_exec($ch);
  40. $resinfo = curl_getinfo($ch);
  41. dump($response);
  42. dump($resinfo);
  43. dump(curl_error($ch));
  44. exit;
  45. curl_close($ch);
  46. if($resinfo["http_code"] != 200){
  47. throw new Exception("response status code is not valid. status code: ".$resinfo["http_code"]);
  48. }
  49. return $response;
  50. }
  51. public static function buildGetUrl($strUrl, $params, $charset){
  52. if ($params == null || count($params) == 0) {
  53. return $strUrl;
  54. }
  55. $buildUrlParams = http_build_query($params);
  56. if(strrpos($strUrl,'?',0) != (strlen($strUrl) + 1)){ //最后是否以?结尾
  57. return $strUrl.'?'.$buildUrlParams;
  58. }
  59. return $strUrl.$buildUrlParams;
  60. }
  61. public static function buildOrderedSignStr($path,$params){
  62. $isSorted=ksort($params);
  63. $comSignStr = $path.'?';
  64. $hasParam = false;
  65. foreach ($params as $key => $value) {
  66. if(null == $key || "" == $key || null == $value || ""==$value){
  67. }else{
  68. if ($hasParam) {
  69. $comSignStr=$comSignStr.'&';
  70. }else{
  71. $hasParam=true;
  72. }
  73. $comSignStr=$comSignStr.$key.'='.$value;
  74. }
  75. }
  76. return $comSignStr;
  77. }
  78. public static function buildForm($url,$params){
  79. $buildedFields = self::buildHiddenFields($params);
  80. return '<form name="auto_submit_form" method="post" action="'.$url.'">'."\n".$buildedFields.'<input type="submit" value="立刻提交" style="display:none" >'."\n".'</form>'."\n".'<script>document.forms[0].submit();</script>';
  81. }
  82. public static function buildHiddenFields($params){
  83. if ($params == null || count($params) == 0) {
  84. return '';
  85. }
  86. $result = '';
  87. foreach ($params as $key => $value) {
  88. if($key == null || $value == null){
  89. continue;
  90. }
  91. $buildfield = self::buildHiddenField($key,$value);
  92. $result = $result.$buildfield;
  93. }
  94. return $result;
  95. }
  96. public static function buildHiddenField($key,$value){
  97. return '<input type="hidden" name="'.$key.'" value="'.preg_replace('/"/', '&quot;', $value).'">'."\n";
  98. }
  99. }
  100. ?>