DefaultIcbcClient.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. //namespace icbc;
  3. include_once 'IcbcConstants.php';
  4. include_once 'IcbcSignature.php';
  5. include_once 'WebUtils.php';
  6. include_once 'IcbcEncrypt.php';
  7. class DefaultIcbcClient{
  8. public $appId;
  9. public $privateKey;
  10. public $signType;
  11. public $charset;
  12. public $format;
  13. public $icbcPulicKey;
  14. public $encryptKey;
  15. public $encryptType;
  16. public $ca;
  17. public $password;
  18. function __construct($appId,$privateKey,$signType,$charset,$format,$icbcPulicKey,
  19. $encryptKey,$encryptType,$ca,$password)
  20. {
  21. $this->appId = $appId;
  22. $this->privateKey = $privateKey;
  23. if($signType == null || $signType == ""){
  24. $this->signType=IcbcConstants::$SIGN_TYPE_RSA;
  25. }else{
  26. $this->signType = $signType;
  27. }
  28. if($charset == null || $charset == ""){
  29. $this->charset = IcbcConstants::$CHARSET_UTF8;
  30. }else{
  31. $this->charset = $charset;
  32. }
  33. if($format == null || $format == ""){
  34. $this->format=IcbcConstants::$FORMAT_JSON;
  35. }else{
  36. $this->format = $format;
  37. }
  38. $this->icbcPulicKey = $icbcPulicKey;
  39. $this->encryptKey = $encryptKey;
  40. $this->encryptType = $encryptType;
  41. $this->password = $password;
  42. // 去除签名数据及证书数据中的空格
  43. if ($ca != null && $ca != "") {
  44. $ca = preg_replace("/\s*|\t/", "", $ca);
  45. }
  46. $this->ca = $ca;
  47. }
  48. function execute($request,$msgId,$appAuthToken){
  49. $params = $this->prepareParams($request, $msgId, $appAuthToken);
  50. //发送请求
  51. //接收响应
  52. if($request["method"] == "GET" ){
  53. $respStr = WebUtils::doGet($request["serviceUrl"],$params,$this->charset);
  54. }elseif ($request["method"] == "POST") {
  55. $respStr = WebUtils::doPost($request["serviceUrl"],$params,$this->charset);
  56. }else{
  57. throw new Exception("Only support GET or POST http method!");
  58. }
  59. //增加了对传回报文中含有中文字符以及反斜杠的转换(json_encode(str,JSON_UNESCAPED_UNICODE(240)+JSON_UNESCAPED_SLASHES(80)=320))
  60. $respBizContentStr = json_encode(json_decode($respStr,true)[IcbcConstants::$RESPONSE_BIZ_CONTENT],320);
  61. $sign = json_decode($respStr,true)[IcbcConstants::$SIGN];
  62. //解析响应
  63. $passed = IcbcSignature::verify($respBizContentStr, IcbcConstants::$SIGN_TYPE_RSA, $this->icbcPulicKey, $this->charset, $sign);
  64. if(!$passed){
  65. throw new Exception("icbc sign verify not passed!");
  66. }
  67. if($request["isNeedEncrypt"]){
  68. $respBizContentStr = IcbcEncrypt::decryptContent(substr($respBizContentStr, 1 , strlen($respBizContentStr)-2), $this->encryptType, $this->encryptKey, $this->charset);
  69. }
  70. //返回解析结果
  71. return $respBizContentStr;
  72. }
  73. function prepareParams($request, $msgId, $appAuthToken){
  74. $bizContentStr = json_encode($request["biz_content"]);
  75. $path = parse_url($request["serviceUrl"],PHP_URL_PATH);
  76. $params = array();
  77. if($request["extraParams"] != null){
  78. $params = array_merge($params,$request["extraParams"]);
  79. }
  80. $params[IcbcConstants::$APP_ID] = $this->appId;
  81. $params[IcbcConstants::$SIGN_TYPE] = $this->signType;
  82. $params[IcbcConstants::$CHARSET] = $this->charset;
  83. $params[IcbcConstants::$FORMAT] = $this->format;
  84. $params[IcbcConstants::$CA] = $this->ca;
  85. $params[IcbcConstants::$APP_AUTH_TOKEN] = $appAuthToken;
  86. $params[IcbcConstants::$MSG_ID] = $msgId;
  87. date_default_timezone_set(IcbcConstants::$DATE_TIMEZONE);
  88. $params[IcbcConstants::$TIMESTAMP] = date(IcbcConstants::$DATE_TIME_FORMAT);
  89. if ($request["isNeedEncrypt"]){
  90. if ($bizContentStr != null) {
  91. $params[IcbcConstants::$ENCRYPT_TYPE] = $this->encryptType;
  92. $params[IcbcConstants::$BIZ_CONTENT_KEY] = IcbcEncrypt::encryptContent($bizContentStr, $this->encryptType, $this->encryptKey, $this->charset);
  93. }
  94. } else {
  95. $params[IcbcConstants::$BIZ_CONTENT_KEY] = $bizContentStr;
  96. }
  97. $strToSign = WebUtils::buildOrderedSignStr($path, $params);
  98. $signedStr = IcbcSignature::sign($strToSign, $this->signType, $this->privateKey, $this->charset, $this->password);
  99. $params[IcbcConstants::$SIGN] = $signedStr;
  100. return $params;
  101. }
  102. function JSONTRANSLATE($array) {
  103. foreach ($array as $key => $value){
  104. $array[$key] = urlencode($value);
  105. }
  106. return json_encode($array);
  107. }
  108. function encodeOperations ($array) {
  109. foreach ((array)$array as $key => $value) {
  110. if (is_array($value)) {
  111. $this->encodeOperations($array[$key]);
  112. } else {
  113. $array[$key] = urlencode(mb_convert_encoding($value,'UTF-8','GBK'));
  114. }
  115. }
  116. return $array;
  117. }
  118. }
  119. ?>