test.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. *HttpGateWayDemo测试类
  4. *
  5. */
  6. class HttpGateWayDemo {
  7. const PUK_KEY = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCEMt8QR5miQjxNJQPh0ruKiq6KIaK9g7Rd3NPE7YfdHUgJwMQy760ddqesNmg2wNAzH2lezzFYjZp2RFxIHaeY2ZTvJnQAazfR8x9UiQ1u+Pz0RyouAhCzIFdLknRMdx/zwOPiYeI2KCZPNCVJFdTgpbo0prCwzi1qXMZEPWasewIDAQAB';
  8. const IV = '0123456789ABEDEF'; //IV参数必须是16位。
  9. const USERNAME = 'test103';
  10. const PASSWORD = '123456';
  11. const OSP_TOKEN_URL_TEST = 'http://223.160.16.24:8080/oauth';
  12. const OSP_TOKEN_URL_PRO = 'https://partner.10099.com.cn/oauth';
  13. const BIZ_URL_TEST = 'http://223.160.16.24:8080/gwec-contact-web/openapi';
  14. const BIZ_URL_PRO = 'https://partner.10099.com.cn/openapi';
  15. private $private_key = "-----BEGIN RSA PRIVATE KEY-----
  16. ......
  17. -----END RSA PRIVATE KEY-----";
  18. private $public_key = "-----BEGIN PUBLIC KEY-----
  19. MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCEMt8QR5miQjxNJQPh0ruKiq6K
  20. IaK9g7Rd3NPE7YfdHUgJwMQy760ddqesNmg2wNAzH2lezzFYjZp2RFxIHaeY2ZTv
  21. JnQAazfR8x9UiQ1u+Pz0RyouAhCzIFdLknRMdx/zwOPiYeI2KCZPNCVJFdTgpbo0
  22. prCwzi1qXMZEPWasewIDAQAB
  23. -----END PUBLIC KEY-----";
  24. public $pubkey;
  25. public $privkey;
  26. function __construct() {
  27. // 获得资源类型公钥和私钥,
  28. $this->privkey = openssl_pkey_get_private($this->private_key);
  29. $this->pubkey = openssl_pkey_get_public($this->public_key);
  30. }
  31. /**
  32. * rsa公钥加密
  33. */
  34. public function rsa_encrypt($data) {
  35. if (openssl_public_encrypt($data, $encrypted, $this->pubkey)){
  36. //由于加密后为二进制数据,为了展示和传输,base64_en一下,解密同
  37. $data = base64_encode($encrypted);
  38. }
  39. else{
  40. echo 'encrypt wrong';
  41. }
  42. return $data;
  43. }
  44. /**
  45. * rsa私钥解密
  46. */
  47. public function rsa_decrypt($data) {
  48. if (openssl_private_decrypt(base64_decode($data), $decrypted, $this->privkey)){
  49. $data = $decrypted;
  50. }
  51. else{
  52. echo 'wrong decrypt';
  53. }
  54. return $data;
  55. }
  56. /**
  57. * aes加密
  58. */
  59. public function aes_encrypts($data,$randoms)
  60. {
  61. //php7.1 以上版本用法
  62. return base64_encode(openssl_encrypt($data, "AES-192-CBC", $randoms, OPENSSL_RAW_DATA, self::IV));
  63. }
  64. /**
  65. * aes解密
  66. */
  67. public function aes_decrypts($data,$randoms)
  68. {
  69. //php7.1 以上版本用法
  70. return openssl_decrypt(base64_decode($data), "AES-192-CBC", $randoms, OPENSSL_RAW_DATA, self::IV);
  71. }
  72. function encode($data){
  73. $key = openssl_pkey_get_public(RSA_PUBLIC);
  74. if (!$key) {
  75. return('公钥不可用');
  76. }
  77. $_ret = openssl_public_encrypt($data, $crypted, $key);
  78. if (!$_ret) {
  79. return('加密失败,请检查RSA秘钥');
  80. }
  81. return base64_encode($crypted);
  82. }
  83. /**
  84. * gettoken
  85. * @param 无
  86. * @return token string
  87. */
  88. public function gettoken()
  89. {
  90. $arr['username']=self::USERNAME;
  91. $arr['password']=self::PASSWORD;
  92. $arr['sign']=md5($arr['username'].'&'.$arr['password']);
  93. echo '<br>username:';
  94. var_dump($arr);
  95. $url=self::OSP_TOKEN_URL_TEST.'?username='.$arr['username'].'&password='.$arr['password'].'&sign='.$arr['sign'];
  96. $strs=file_get_contents($url);
  97. $re=json_decode($strs,true);
  98. echo '<br>token:';
  99. var_dump($re);
  100. return $re['token'];
  101. }
  102. /**
  103. *处理请求头信息
  104. * initSecrityHead
  105. * @param $radoms string
  106. * @return $head_str array
  107. */
  108. public function initSecrityHead($radoms) {
  109. $ajaxran = $this->rsa_encrypt($radoms);
  110. $head_str[] = "Content-type: application/json;charset=UTF-8";
  111. $head_str[] = "ostype: android";
  112. $head_str[] = "publicKey: ".self::PUK_KEY;
  113. $head_str[] = "ajaxrandomcode: ".$ajaxran;
  114. $head_str[] = "CRC: 123456";
  115. $head_str[] = "appCode: JS0020000091";
  116. return $head_str;
  117. }
  118. /**
  119. *curl post 请求
  120. * jsonpost
  121. * @param $jsonbody array
  122. * @param $url string
  123. * @return array
  124. */
  125. public function jsonpost($jsonbody,$url)
  126. {
  127. $random='12345678';
  128. $randoms=$random.$random.$random;
  129. $jsonbody_str = json_encode($jsonbody);
  130. //通过AES对业务参数进行加密
  131. $s = $this->aes_encrypts($jsonbody_str, $randoms);
  132. $params['requestParams'] = $s;
  133. $ch = curl_init();
  134. $head_str = $this->initSecrityHead($random);
  135. echo '<br>请求头:';
  136. var_dump($head_str);
  137. $params_str = json_encode($params);
  138. echo '<br>json格式:';
  139. var_dump($params_str);
  140. // 返回 response_header, 该选项非常重要,如果不为 true, 只会获得响应的正文
  141. // curl_setopt($ch, CURLOPT_HEADER, true);
  142. curl_setopt($ch, CURLOPT_POST, 1);
  143. curl_setopt($ch, CURLOPT_URL, $url);
  144. curl_setopt($ch, CURLOPT_POSTFIELDS, $params_str);
  145. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  146. curl_setopt($ch, CURLOPT_HTTPHEADER, $head_str);
  147. curl_setopt($ch, CURLINFO_HEADER_OUT, true);
  148. $response = curl_exec($ch);
  149. $httpCode = curl_getinfo($ch);
  150. echo '<br>返回值:';
  151. var_dump($httpCode);
  152. echo '<br>';
  153. curl_close($ch);
  154. return $response;
  155. }
  156. //调用业务接口
  157. public function qrynumber()
  158. {
  159. $token=$this->gettoken();
  160. $arr['regionId']='025';
  161. $arr['currentPage']='1';
  162. $arr['pageSize']='30';
  163. $url=self::BIZ_URL_TEST.'/fenxiao/openAcount/qryNumberNew?token='.$token;
  164. echo '<br>请求url:';
  165. echo $url;
  166. $result= $this->jsonpost($arr,$url);
  167. var_dump($result);
  168. }
  169. }
  170. //调用demo执行
  171. $test = new HttpGateWayDemo();
  172. $test->qrynumber();