3 Star 6 Fork 0

wlphp / jiamiapi

Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Without author's permission, this code is only for learning and cannot be used for other purposes.
Clone or download
client.php 9.29 KB
Copy Edit Web IDE Raw Blame History
wlphp authored 2017-08-22 18:01 . no commit message
<?php
header("Content-type: text/html; charset=utf-8");
/*封装调取加密接口方法*/
/*******拼接接口url开始********/
$php_self=$_SERVER['PHP_SELF'];
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$fronturl=$protocol.$_SERVER['HTTP_HOST'].substr($php_self,0,strrpos($php_self,'/'));
$sendurl =$fronturl."/api.php/Api/index"; //接口地址入口
/*******拼接接口url结束********/
$sendjson = '[{"action":"test","username":"zhangsan","password":"1111111"}]';
$sendfile['img1'] = "@D:/WWW/jiamiapi/img1.jpg"; //上传的文件切记不要忘记加@
$sendfile['img2'] = "@D:/WWW/jiamiapi/img2.jpg";
//$returnjson = sendtointerface($sendurl, $sendjson, $sendfile); //文件上传示例,文件不采用加密模式
$returnjson = sendtointerface($sendurl, $sendjson); //不包含文件上传示例
echo $returnjson;
/**
* @param $sendurl 接口url
* @param array $sendjson 发送json数据
* @param array $sendfile 发送的文件
* @return mixed|string
*/
function sendtointerface($sendurl, $sendjson = array(), $sendfile = array())
{
$private_key = '-----BEGIN RSA PRIVATE KEY-----
MIICWwIBAAKBgQCwL+LZS88pi8wb5yrTqvBnJPlFa//gLHcQcogyqzpimtE9yDxf
8QpABXs4+fay6iWQz3ogCI/UxTHjn/FwVfIhoHZVQtdti9tSb9fqMtNTCWp/xw89
iIriP10RQkz34UvT9rvz0grSWoSu/k4pCcUhFxFntmWNdc5HTxkYYVM6UQIDAQAB
AoGAZ1MN9GVTDohfoRWGti+PYM38KcxWeP9arBie6ygjy2QDxfnB/nn7iuQsCZP+
K5rJgQ1MD7E1uJAU2+sWysx8fGYOctXp0fzNEAGcTYPC3F1dueUdZRKyi887OK2L
eQsykC4FG8w5KyveQMjwYguhpiPew942/U0OJdNouAvJoukCQQDmm1avcW5PZdDv
uK2YiA+YI4eRScsLNfJX5RN0ccHBpLARBP1thzWUT9rhJLlDM4bdiMRj3v7a37xZ
wcQcf5NbAkEAw5Z9OKJpLaAJQXtqDizxWolotG/r9GphpRt161ljnLs/H2bVqoMp
2xJchL4LjsDoo3Xo3hpc3L5u0BqSwb60wwJACaxIZZGPe/scWYHYnkZOySXsYNbE
kpLPuIoeV8sRwoqi7APWkYqTX8Y9uyXsM3ps1MwQ2NOYwxuRbf5nuH5NcwJABN4W
6bTpBzCnalDG9+lYbFdlGrB6X8/R9Ej1q1DDMOBcFsz5Xk/7V69iTA9qvDfC0maZ
0AU58Ghw2479vgKf/wJARJ5bX9h5ihZGmh3hh0aaIHKDilg3k2B3vwumzkLKVxCd
8i6l/dRjO3JvdBnTHC11Jt2zQ9LkHklfVzfx8dGvqw==
-----END RSA PRIVATE KEY-----';
$public_key = '-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwL+LZS88pi8wb5yrTqvBnJPlF
a//gLHcQcogyqzpimtE9yDxf8QpABXs4+fay6iWQz3ogCI/UxTHjn/FwVfIhoHZV
Qtdti9tSb9fqMtNTCWp/xw89iIriP10RQkz34UvT9rvz0grSWoSu/k4pCcUhFxFn
tmWNdc5HTxkYYVM6UQIDAQAB
-----END PUBLIC KEY-----';
$private_key_pkcs8 = '-----BEGIN PRIVATE KEY-----
MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBALAv4tlLzymLzBvn
KtOq8Gck+UVr/+AsdxByiDKrOmKa0T3IPF/xCkAFezj59rLqJZDPeiAIj9TFMeOf
8XBV8iGgdlVC122L21Jv1+oy01MJan/HDz2IiuI/XRFCTPfhS9P2u/PSCtJahK7+
TikJxSEXEWe2ZY11zkdPGRhhUzpRAgMBAAECgYBnUw30ZVMOiF+hFYa2L49gzfwp
zFZ4/1qsGJ7rKCPLZAPF+cH+efuK5CwJk/4rmsmBDUwPsTW4kBTb6xbKzHx8Zg5y
1enR/M0QAZxNg8LcXV255R1lErKLzzs4rYt5CzKQLgUbzDkrK95AyPBiC6GmI97D
3jb9TQ4l02i4C8mi6QJBAOabVq9xbk9l0O+4rZiID5gjh5FJyws18lflE3RxwcGk
sBEE/W2HNZRP2uEkuUMzht2IxGPe/trfvFnBxBx/k1sCQQDDln04omktoAlBe2oO
LPFaiWi0b+v0amGlG3XrWWOcuz8fZtWqgynbElyEvguOwOijdejeGlzcvm7QGpLB
vrTDAkAJrEhlkY97+xxZgdieRk7JJexg1sSSks+4ih5XyxHCiqLsA9aRipNfxj27
JewzemzUzBDY05jDG5Ft/me4fk1zAkAE3hbptOkHMKdqUMb36VhsV2UasHpfz9H0
SPWrUMMw4FwWzPleT/tXr2JMD2q8N8LSZpnQBTnwaHDbjv2+Ap//AkBEnltf2HmK
FkaaHeGHRpogcoOKWDeTYHe/C6bOQspXEJ3yLqX91GM7cm90GdMcLXUm3bND0uQe
SV9XN/Hx0a+r
-----END PRIVATE KEY-----';
$content = $sendjson;
$randstr = random(16);
$iv = $randstr;
$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $randstr, $content, MCRYPT_MODE_CBC, $iv);
$msg = base64_encode($encrypted);
$pu_key = openssl_pkey_get_public($public_key);//这个函数可用来判断公钥是否是可用的
openssl_public_encrypt($randstr, $encrypted, $pu_key);//公钥加密
$encrypted = base64_encode($encrypted);
$code = $encrypted;
$pi_key = openssl_pkey_get_private($private_key);//这个函数可用来判断私钥是否是可用的,可用返回资源id Resource id
openssl_private_encrypt($msg, $encrypted, $pi_key);//私钥加密
$encrypted = base64_encode($encrypted);//加密后的内容通常含有特殊字符,需要编码转换下,在网络间通过url传输时要注意base64编码是否是url安全的
$sign = $encrypted;
$sign = sign($msg, $private_key); //签名函数
$url = $sendurl;
$data = $sendfile; //文件数据
$data['msg'] = urlencode($msg);
$data['sign'] = urlencode($sign);
$data['code'] = urlencode($code);
$content = curlPost($url, $data); //通过curl发送数据到接口
$content = str_replace(array("\r\n", "\r", "\n", "\0"), "", $content); //替换掉特殊字符
$arr = json_decode($content, 1);
//获取接口返回的加密sign、code、msg
$resign = $arr['sign'];
$recode = $arr['code'];
$remsg = $arr['msg'];
openssl_private_decrypt(base64_decode($recode), $decrypted, $pi_key);//私钥解密
$returnrandstr = $decrypted;
$encryptedData = base64_decode($remsg);
@$decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $returnrandstr, $encryptedData, MCRYPT_MODE_CBC, $returnrandstr);
$returnjson = $decrypted;
$returnjson = str_replace(array("\r\n", "\r", "\n", "\0"), "", $returnjson);
$sign = sign($remsg, $private_key);
if ($sign != $resign) {
$returnjson = '[{"sta":"0","msg":"签名验证失败客户端!"}]';
return $returnjson;
}
return $returnjson;
}
/**
* 发送http的get请求
* @param $url 请求地址
* @return mixed
*/
function httpGet($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
// 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
// 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
/**
* @param $url 发送post请求的url
* @param $data 发送的数据
* @return mixed
*/
function curlPost($url, $data)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); //Problem (2) in the Chunked-Encoded data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
@curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); //php5.6.0开始,需要加上这行代码方可上传,否则取不到文件
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);
$errorno = curl_errno($ch);
if ($errorno) {
$rt['sta'] = "0";
$rt['msg'] = "curl错误:$errorno";
return json_encode($rt);
die;
}
return $tmpInfo;
}
/**
* 随机字符
* @param number $length 长度
* @param string $type 类型
* @param number $convert 转换大小写
* @return string
*/
function random($length = 6, $type = 'string', $convert = 0)
{
$config = array(
'number' => '1234567890',
'letter' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
'string' => 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789',
'all' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
);
if (!isset($config[$type])) $type = 'string';
$string = $config[$type];
$code = '';
$strlen = strlen($string) - 1;
for ($i = 0; $i < $length; $i++) {
$code .= $string{mt_rand(0, $strlen)};
}
if (!empty($convert)) {
$code = ($convert > 0) ? strtoupper($code) : strtolower($code);
}
return $code;
}
/**
*签名数据:
*data:utf-8编码的订单原文,
*privatekeyFile:私钥路径
*passphrase:私钥密码
*返回:base64转码的签名数据
*/
function sign($data, $privatekey)
{
$signature = '';
$res = openssl_get_privatekey($privatekey);
openssl_sign($data, $signature, $res);
openssl_free_key($res);
return base64_encode($signature);
}
/**
* 验证签名:
*data:原文
*signature:签名
*publicKeyPath:公钥路径
*返回:签名结果,true为验签成功,false为验签失败
*/
function verity($data, $signature, $pubKey)
{
$res = openssl_get_publickey($pubKey);
$result = (bool)openssl_verify($data, base64_decode($signature), $res);
openssl_free_key($res);
return $result;
}
/**
* 文本记录函数
* @param string $word 输入记录的值
*/
function logRes($word = '')
{
$logname = "./log/" . date("Ymd") . ".txt";
$fp = fopen($logname, "a");
flock($fp, LOCK_EX);
fwrite($fp, "执行日期:" . strftime("%Y-%m-%d %H:%M:%S", time()) . "\r\n" . $word . "\r\n");
flock($fp, LOCK_UN);
fclose($fp);
}

Comment ( 0 )

Sign in for post a comment

PHP
1
https://git.oschina.net/wlphp/jiamiapi.git
git@git.oschina.net:wlphp/jiamiapi.git
wlphp
jiamiapi
jiamiapi
master

Search