<?php

/**
 * rsa
 * 参数rsa加密
 * $params 请求参数(注意传过来的数组参数键名和请求参数要相同)
 * $method="Post" 请求类型 默认为:Post/Get/Put/Delete
 */
 function rsa($params,$method = "Post")
 {
       // 请求参数
       $Spare = array();
       $arrSig = array();

     $priKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCJfTvFrDo2H5pSvrm0ijCnciljtjCwKn+yj8i8HaFi3BYkiniipFJwJLzOTr5VxL74nUcqNsF1syjv7FfCfE18JNy65bH+6cHmitKYEKxIe4Qc9uZ2KEjTwqJmSR7ECLa/lGp7p1Ld24oiOz5FMQS+lt5HDKm1Wz1ONkuClO13yQIDAQAB";

     $key    = strtoupper('ABCDWFGSSW');

     $arrSig['userId'] = 'quanxun';        //账号
     $userPwd= '12345678';                        //密码

     $requestType = 'PHP';                          //语言

     $priKey = chunk_split($priKey,64,"\n");//转换为pem格式的公钥
     $priKey = "-----BEGIN PUBLIC KEY-----\n{$priKey}-----END PUBLIC KEY-----\n";
     $publicKey = openssl_pkey_get_public($priKey);//获取公钥内容
     $flag = openssl_public_encrypt($key,$secretKey,$publicKey);//用openssl加密

     if($flag)
     {
         openssl_public_encrypt($requestType,$requestType2,$publicKey);
         openssl_public_encrypt($userPwd,$userPwd2,$publicKey);

         $arrSig['secretKey'] =  base64_encode($secretKey);//base64编码
         $arrSig['requestType'] = base64_encode($requestType2);
         $arrSig['userPwd'] = base64_encode($userPwd2);
         if (!empty($params))
         {
             foreach ($params as $k => $v)
             {
                 $arrSig[$k] = iconv("UTF-8", "UTF-8//IGNORE", $v);

                 openssl_public_encrypt($arrSig[$k],$Spare[$k],$publicKey);
                 $arrSig[$k] = base64_encode($Spare[$k]);
             }
         }
     }

   return $arrSig;
 }
/*
 * curl 请求接口
 * $url    请求路径
 * $data   请求数据
 * memthon = POST
 */
function curl_post($url,$data=array(),$type="",$timeout=10,$header=array())
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    if($timeout>0)   curl_setopt($ch, CURLOPT_TIMEOUT,$timeout);
    if(strstr($url,"https://")){
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    }
    $user_agent = "ZhuShangDai Curl/1.0";
    curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
    if(!empty($data)) {
        $querystring = "";
        if (is_array($data)){
            $querystring = http_build_query($data);
        } else {
            $querystring = $data;
        }
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $querystring);
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    $header=$header?$header:array();
    if($type=="json"){
        $header[]="Content-Type: application/json; charset=utf-8";
        $header[]="Cache-Control: no-cache";
    }
    if($type=="json"){
        $header[]="Content-Type: application/json; charset=utf-8";
        $header[]="Cache-Control: no-cache";
    }elseif($type=="xml"){
        $header[]="Content-Type: text/xml; charset=utf-8";
    }

    if(!empty($header)){
        curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
    }

    $result=curl_exec($ch);
    $error = curl_error($ch);
    curl_close ($ch);
    return $error ? $error : $result;
}
?>
