/**
* @name ssl Curl Post数据
* @param string $url 接收数据的api
* @param string $vars 提交的数据
* @param int $second 要求程序必须在$second秒内完成,负责到$second秒后放到后台执行
* @return string or boolean 成功且对方有返回值则返回
* 由于php的curl只支持pem格式、der、eng格式,而之前生成的是p12的格式,所以需要转换一下
*PKCS#12 到 PEM 的转换
*openssl pkcs12 -nocerts -nodes -in cert.p12 -out private.pem
*验证
*openssl pkcs12 -clcerts -nokeys -in cert.p12 -out cert.pem
*/
function curlSsl($url, $vars, $aHeader = array(),$type= 'post',$code=false, $second = 10)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, '1'); //debug
curl_setopt($ch, CURLOPT_TIMEOUT, $second); // 超时时间
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 是否要求返回数据
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 是否检测服务器的证书是否由正规浏览器认证过的授权CA颁发的
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 是否检测服务器的域名与证书上的是否一致
curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM'); // 证书类型,"PEM" (default), "DER", and"ENG".
curl_setopt($ch, CURLOPT_SSLCERT, config('secure.cert')); // 证书存放路径
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, '****'); // 证书密码
// curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM'); // 私钥类型,"PEM" (default), "DER", and"ENG".
// curl_setopt($ch, CURLOPT_SSLKEY,''); // 私钥存放路径
//设置header信息
if (count($aHeader) >= 1) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
}
// 判断请求类型
if ($type == 'put'){
curl_setopt($ch, CURLOPT_PUT, true);
}elseif ($type == 'delete'){
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
}elseif ($type == 'get'){
curl_setopt($ch, CURLOPT_HTTPGET, true);
}elseif($type == 'post'){
// 通过post请求
curl_setopt($ch, CURLOPT_POST, true);
}
// 提交的数据
if ($vars){
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
}
$data = curl_exec($ch);
$httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
curl_close($ch);
if (!$data && $httpCode === 200 && $code) {
return $httpCode;
}
if ($data){
return $data;
}
else{
return false;
}
}
PHP使用curl向电信平台接口发送数据
最新推荐文章于 2021-03-10 03:35:13 发布