WebUtils.php 3.6 KB

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