[PHP]存一下各种http/https携带参数不携带参数的访问函数

作者: pengpeng 分类: 所有文章 发布时间: 2018-07-18 20:53
/*
*   普通get访问
*/
function http_get($url){
$curl = curl_init();  
curl_setopt($curl, CURLOPT_URL, $url);  
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
$res=json_decode(curl_exec($curl),true);  
curl_close($curl);  
return $res;  
}
/*
*   https get访问
*/
function https_get($url){
$curl = curl_init();  
curl_setopt($curl, CURLOPT_URL, $url);  
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);// https请求不验证证书和hosts  
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);  
$res=json_decode(curl_exec($curl),true);  
curl_close($curl);  
return $res;  
}
/*
*   http post访问
*/
function http_post($url,$post_data){   
$ch = curl_init();  
curl_setopt($ch, CURLOPT_URL, $url);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
// post数据  
curl_setopt($ch, CURLOPT_POST, 1);  
// post的变量  
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);  
$output=json_decode(curl_exec($ch),true);  
curl_close($ch);  
return $data;  
}
/*
https post访问
*/
function https_post($url,$post_data) {
$ch = curl_init();  
curl_setopt($ch, CURLOPT_URL, $url);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
// post数据  
curl_setopt($ch, CURLOPT_POST, 1);  
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts  
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);  
// post的变量  
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);  
$output=json_decode(curl_exec($ch),true);  
curl_close($ch);  
return $output;  
}
/*
*   携带证书的访问
*/
function curl_post_ssl($url, $vars, $second=30,$aHeader=array()){
$certPath   =   getCertPATH();
$ch = curl_init();
//超时时间
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);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
//cert 与 key 分别属于两个.pem文件
//请确保您的libcurl版本是否支持双向认证,版本高于7.20.1
curl_setopt($ch,CURLOPT_SSLCERT,$certPath.DIRECTORY_SEPARATOR.'apiclient_cert.pem');
curl_setopt($ch,CURLOPT_SSLKEY,$certPath.DIRECTORY_SEPARATOR.'apiclient_key.pem');
curl_setopt($ch,CURLOPT_CAINFO,$certPath.DIRECTORY_SEPARATOR.'rootca.pem');
if( count($aHeader) >= 1 ){
curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
}
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
$data = curl_exec($ch);
if($data){
curl_close($ch);
return $data;
}
else {
$error = curl_errno($ch);
echo "call faild, errorCode:$error\n";
curl_close($ch);
return false;
}
}

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

一条评论
  • 百万链

    2018年7月24日 上午10:16

    有幸访问到贵站,能换个友链吗!

发表评论

电子邮件地址不会被公开。 必填项已用*标注