123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <?php
- /**
- *HttpGateWayDemo测试类
- *
- */
- class HttpGateWayDemo {
- const PUK_KEY = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCEMt8QR5miQjxNJQPh0ruKiq6KIaK9g7Rd3NPE7YfdHUgJwMQy760ddqesNmg2wNAzH2lezzFYjZp2RFxIHaeY2ZTvJnQAazfR8x9UiQ1u+Pz0RyouAhCzIFdLknRMdx/zwOPiYeI2KCZPNCVJFdTgpbo0prCwzi1qXMZEPWasewIDAQAB';
- const IV = '0123456789ABEDEF'; //IV参数必须是16位。
- const USERNAME = 'test103';
- const PASSWORD = '123456';
- const OSP_TOKEN_URL_TEST = 'http://223.160.16.24:8080/oauth';
- const OSP_TOKEN_URL_PRO = 'https://partner.10099.com.cn/oauth';
- const BIZ_URL_TEST = 'http://223.160.16.24:8080/gwec-contact-web/openapi';
- const BIZ_URL_PRO = 'https://partner.10099.com.cn/openapi';
- private $private_key = "-----BEGIN RSA PRIVATE KEY-----
- ......
- -----END RSA PRIVATE KEY-----";
- private $public_key = "-----BEGIN PUBLIC KEY-----
- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCEMt8QR5miQjxNJQPh0ruKiq6K
- IaK9g7Rd3NPE7YfdHUgJwMQy760ddqesNmg2wNAzH2lezzFYjZp2RFxIHaeY2ZTv
- JnQAazfR8x9UiQ1u+Pz0RyouAhCzIFdLknRMdx/zwOPiYeI2KCZPNCVJFdTgpbo0
- prCwzi1qXMZEPWasewIDAQAB
- -----END PUBLIC KEY-----";
- public $pubkey;
- public $privkey;
- function __construct() {
- // 获得资源类型公钥和私钥,
- $this->privkey = openssl_pkey_get_private($this->private_key);
- $this->pubkey = openssl_pkey_get_public($this->public_key);
- }
- /**
- * rsa公钥加密
- */
- public function rsa_encrypt($data) {
- if (openssl_public_encrypt($data, $encrypted, $this->pubkey)){
- //由于加密后为二进制数据,为了展示和传输,base64_en一下,解密同
- $data = base64_encode($encrypted);
- }
- else{
- echo 'encrypt wrong';
- }
- return $data;
- }
- /**
- * rsa私钥解密
- */
- public function rsa_decrypt($data) {
- if (openssl_private_decrypt(base64_decode($data), $decrypted, $this->privkey)){
- $data = $decrypted;
- }
- else{
- echo 'wrong decrypt';
- }
- return $data;
- }
- /**
- * aes加密
- */
- public function aes_encrypts($data,$randoms)
- {
- //php7.1 以上版本用法
- return base64_encode(openssl_encrypt($data, "AES-192-CBC", $randoms, OPENSSL_RAW_DATA, self::IV));
- }
- /**
- * aes解密
- */
- public function aes_decrypts($data,$randoms)
- {
- //php7.1 以上版本用法
- return openssl_decrypt(base64_decode($data), "AES-192-CBC", $randoms, OPENSSL_RAW_DATA, self::IV);
- }
- function encode($data){
- $key = openssl_pkey_get_public(RSA_PUBLIC);
- if (!$key) {
- return('公钥不可用');
- }
- $_ret = openssl_public_encrypt($data, $crypted, $key);
- if (!$_ret) {
- return('加密失败,请检查RSA秘钥');
- }
- return base64_encode($crypted);
- }
- /**
- * gettoken
- * @param 无
- * @return token string
- */
- public function gettoken()
- {
- $arr['username']=self::USERNAME;
- $arr['password']=self::PASSWORD;
- $arr['sign']=md5($arr['username'].'&'.$arr['password']);
- echo '<br>username:';
- var_dump($arr);
- $url=self::OSP_TOKEN_URL_TEST.'?username='.$arr['username'].'&password='.$arr['password'].'&sign='.$arr['sign'];
- $strs=file_get_contents($url);
- $re=json_decode($strs,true);
- echo '<br>token:';
- var_dump($re);
- return $re['token'];
- }
- /**
- *处理请求头信息
- * initSecrityHead
- * @param $radoms string
- * @return $head_str array
- */
- public function initSecrityHead($radoms) {
- $ajaxran = $this->rsa_encrypt($radoms);
- $head_str[] = "Content-type: application/json;charset=UTF-8";
- $head_str[] = "ostype: android";
- $head_str[] = "publicKey: ".self::PUK_KEY;
- $head_str[] = "ajaxrandomcode: ".$ajaxran;
- $head_str[] = "CRC: 123456";
- $head_str[] = "appCode: JS0020000091";
- return $head_str;
- }
- /**
- *curl post 请求
- * jsonpost
- * @param $jsonbody array
- * @param $url string
- * @return array
- */
- public function jsonpost($jsonbody,$url)
- {
- $random='12345678';
- $randoms=$random.$random.$random;
- $jsonbody_str = json_encode($jsonbody);
- //通过AES对业务参数进行加密
- $s = $this->aes_encrypts($jsonbody_str, $randoms);
- $params['requestParams'] = $s;
- $ch = curl_init();
- $head_str = $this->initSecrityHead($random);
- echo '<br>请求头:';
- var_dump($head_str);
- $params_str = json_encode($params);
- echo '<br>json格式:';
- var_dump($params_str);
- // 返回 response_header, 该选项非常重要,如果不为 true, 只会获得响应的正文
- // curl_setopt($ch, CURLOPT_HEADER, true);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $params_str);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $head_str);
- curl_setopt($ch, CURLINFO_HEADER_OUT, true);
- $response = curl_exec($ch);
- $httpCode = curl_getinfo($ch);
- echo '<br>返回值:';
- var_dump($httpCode);
- echo '<br>';
- curl_close($ch);
- return $response;
- }
- //调用业务接口
- public function qrynumber()
- {
- $token=$this->gettoken();
- $arr['regionId']='025';
- $arr['currentPage']='1';
- $arr['pageSize']='30';
- $url=self::BIZ_URL_TEST.'/fenxiao/openAcount/qryNumberNew?token='.$token;
- echo '<br>请求url:';
- echo $url;
- $result= $this->jsonpost($arr,$url);
- var_dump($result);
- }
- }
- //调用demo执行
- $test = new HttpGateWayDemo();
- $test->qrynumber();
|