<?php //https协议是否需要验证证书 define('SSL_VERIFYPEER', true); $key = '您申请的key,在会员中心->我的数据->对应数据的下方'; //请确认您的key为新版本的key,新版本key为32位;旧版本16位; $url="https://v.1dq.com/api/c43"; $param["appid"]="您的appid,在会员中心->安全设置->对应数据->appid"; $param['bankcard'] = '123456789'; $param['apiversion'] = '2.0.5'; $calldata = APIStore::curl($url,$param,$key); var_dump($calldata); /** * APIStore * Class APIStore */ class APIStore { /** * 获取远程内容 * @param $url * @param null $param * @param $key * @return mixed */ static public function curl($url, $param, $key) { //aes $aes = new Aes($key); $sign = $aes->encrypt(json_encode($param, JSON_UNESCAPED_UNICODE)); //重新组建数组 $params['appid'] = $param['appid']; $params['sign'] = $sign; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); //如果是https协议 if (stripos($url, "https://") !== FALSE) { /** * 如果需要验证证书 */ if (SSL_VERIFYPEER) { //验证交换证书 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); //检查SSL证书公用名是否存在,并且是否与提供的主机名匹配 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); //设置实现协议为TLS1.0版本 curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); //根证书文件路径,相对路径和绝对路径均可, //推荐使用绝对路径;为了安全证书文件最好不要和应用代码放在一起; //用户请保持更新 使用https接口需要设置该证书文件为可信任根证书, //以最大限度满足安全性(使用信任任何证书的方式并不安全)。 curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem'); } /** * 如果不需要验证证书 */ else { curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); //CURL_SSLVERSION_TLSv1 curl_setopt($curl, CURLOPT_SSLVERSION, 1); } } //USERAGENT curl_setopt($curl, CURLOPT_USERAGENT, 'APIStore'); //超时时间 curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 120); curl_setopt($curl, CURLOPT_TIMEOUT, 120); //通过POST方式提交 curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params)); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //返回内容 $callbcak = curl_exec($curl); //关闭 curl_close($curl); //解密 $callbcakdata = $aes->decrypt($callbcak); //返回内容 return json_decode($callbcakdata ? $callbcakdata : $callbcak, true); } } /** * Class Aes128 */ class Aes { //密钥 private $appkey; public function __construct($key) { $this->appkey = $key; } /** * 加密方法 * @param string $str * @return string */ public function encrypt($str) { //AES, 128 ECB模式加密数据 //$screct_key = $this->appkey; $screct_key = hex2bin($this->appkey); $str = trim($str); $str = $this->addPKCS7Padding($str); $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND); $encrypt_str = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $screct_key, $str, MCRYPT_MODE_CBC, '0000000000000000'); return base64_encode($encrypt_str); } /** * 解密方法 * @param string $str * @return string */ public function decrypt($str) { //AES, 128 ECB模式加密数据 $screct_key = hex2bin($this->appkey); $str = base64_decode($str); //$screct_key = base64_decode($screct_key); $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND); $encrypt_str = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $screct_key, $str, MCRYPT_MODE_CBC, '0000000000000000'); $encrypt_str = $this->stripPKSC7Padding($encrypt_str); return $encrypt_str; } /** * 填充算法 * @param string $source * @return string */ function addPKCS7Padding($source) { $source = trim($source); $block = mcrypt_get_block_size('rijndael-128', 'cbc'); $pad = $block - (strlen($source) % $block); if ($pad <= $block) { $char = chr($pad); $source .= str_repeat($char, $pad); } return $source; } /** * 移去填充算法 * @param string $source * @return string */ function stripPKSC7Padding($source) { $char = substr($source, -1); $num = ord($char); $source = substr($source, 0, -$num); return $source; } }