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; }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM