WebUtils.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. curl_close($ch);
  42. dump($response);
  43. dump($resinfo);
  44. exit;
  45. if($resinfo["http_code"] != 200){
  46. throw new Exception("response status code is not valid. status code: ".$resinfo["http_code"]);
  47. }
  48. return $response;
  49. }
  50. public static function buildGetUrl($strUrl, $params, $charset){
  51. if ($params == null || count($params) == 0) {
  52. return $strUrl;
  53. }
  54. $buildUrlParams = http_build_query($params);
  55. if(strrpos($strUrl,'?',0) != (strlen($strUrl) + 1)){ //最后是否以?结尾
  56. return $strUrl.'?'.$buildUrlParams;
  57. }
  58. return $strUrl.$buildUrlParams;
  59. }
  60. public static function buildOrderedSignStr($path,$params){
  61. $isSorted=ksort($params);
  62. $comSignStr = $path.'?';
  63. $hasParam = false;
  64. foreach ($params as $key => $value) {
  65. if(null == $key || "" == $key || null == $value || ""==$value){
  66. }else{
  67. if ($hasParam) {
  68. $comSignStr=$comSignStr.'&';
  69. }else{
  70. $hasParam=true;
  71. }
  72. $comSignStr=$comSignStr.$key.'='.$value;
  73. }
  74. }
  75. return $comSignStr;
  76. }
  77. public static function buildForm($url,$params){
  78. $buildedFields = self::buildHiddenFields($params);
  79. 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>';
  80. }
  81. public static function buildHiddenFields($params){
  82. if ($params == null || count($params) == 0) {
  83. return '';
  84. }
  85. $result = '';
  86. foreach ($params as $key => $value) {
  87. if($key == null || $value == null){
  88. continue;
  89. }
  90. $buildfield = self::buildHiddenField($key,$value);
  91. $result = $result.$buildfield;
  92. }
  93. return $result;
  94. }
  95. public static function buildHiddenField($key,$value){
  96. return '<input type="hidden" name="'.$key.'" value="'.preg_replace('/"/', '&quot;', $value).'">'."\n";
  97. }
  98. }
  99. ?>