php对接java接口

1、php curl 传参形式

 public function send($url,$postData){
        $ch = curl_init();
        $headers = array("Content-type: application/json;charset='utf-8'",
            "Accept: application/json", "Cache-Control: no-cache", "Pragma: no-cache");
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        $data = curl_exec($ch);

        if ($data === false || $data === null) {
            $error_code = curl_errno($ch);
            curl_close($ch);
            return $error_code;
        } else {
            curl_close($ch);
           return json_decode($data,true);
        }
    }
    public  function send2($url){
        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 0);
        //设置获取的信息以文件流的形式返回,而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //执行命令
        $data = curl_exec($curl);
        //关闭URL请求
        if ($data === false || $data === null) {
            $error_code = curl_errno($curl);
            curl_close($curl);
            return $error_code;
        } else {
            curl_close($curl);
            return json_decode($data,true);
        }
    }

$url = "";
send($url,json_encode([]));//以json body方式传参
$url .="?param1=1&param2=2";
send2($url); //params 传参

 

2、java加密类型、对应php类型

/***********************加密*************************/

     String txt="66666";
        String key="123456781234567812345678"; // 必须24位
        //加密
        String keyStr = key;
        byte key_byte[] = keyStr.getBytes();// 3DES 24 bytes key
        SecretKey k = new SecretKeySpec(key_byte, "DESede");
        javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("DESede");
        cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, k);
        byte[] resultByte = cipher.doFinal(txt.getBytes("utf-8"));

        return new String(resultByte, "UTF-8");    
/***********************解密*************************/
    try {
    //str 加密串 pass 密钥
    SecretKey deskey = new SecretKeySpec(pass.getBytes(), 'DESede');

    Cipher c1 = Cipher.getInstance('DESede');
    c1.init(Cipher.ENCRYPT_MODE, deskey);
    byte[] resultByte = c1.doFinal(Base64.decode(str));
    return new String(resultByte, "UTF-8");
    } catch (Exception e) {

    }

php:

class a{
 public $key = '123456781234567812345678'; 

public $method = 'DES-EDE3';//加密方法 加密模式有:DES-ECB、DES-CBC、DES-CTR、DES-OFB、DES-CFB。

public $options = OPENSSL_RAW_DATA;//数据格式选项(可选)

public $iv = '';//加密初始化向量(可选

public function encrypts($data) {

$result = openssl_encrypt($data, $this->method, $this->key,$this->options); return base64_encode($result);

}

public function decrypt($encrypted) {

$encrypted = base64_decode($encrypted); $sign = openssl_decrypt($encrypted, $this->method, $this->key,$this->options); return $sign; }
}

 

posted @ 2020-05-19 16:53  双木君  Views(2008)  Comments(0Edit  收藏  举报