记录了curl常用的get,post,post_file等功能的常用代码块
curl_get_http
/**
* curl get 请求
* @param string $url 请求地址
* @param array $data 请求数据
* @param array $headers 请求header头数据
* @return int|mixed
*/
function curl_get_http($url, $data=[],$headers=[]){
$curl = curl_init();
if ($data){
$url .= '?'.http_build_query($data);
}
curl_setopt($curl, CURLOPT_URL, $url);
if ($headers) curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
$output = curl_exec($curl);
if($output === false) {
throw new Exception(sprintf("%s[%s]",curl_errno($curl), curl_error($curl)));
}
curl_close($curl);
return $output;
}
curl_post_http
/**
* curl post 请求
* @param string $url 请求地址
* @param array $data 请求数据
* @return int|mixed
* @throws
*/
function curl_post_http($url, $data){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
$output = curl_exec($curl);
if($output === false) {
throw new Exception(sprintf("%s[%s]",curl_errno($curl), curl_error($curl)));
}
curl_close($curl);
return $output;
}
curl_post_https
/**
* curl post 请求
* @param string $url 请求地址
* @param array $data 请求数据
* @return int|mixed
*/
function curl_post_https($url, $data){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);//严格校验
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);//严格校验
curl_setopt($curl, CURLOPT_SSLCERTTYPE, 'PEM');
curl_setopt($curl, CURLOPT_SSLCERT, 'cert.crt');
curl_setopt($curl, CURLOPT_SSLCERTPASSWD, '62090');
curl_setopt($curl, CURLOPT_SSLKEYTYPE, 'PEM');
curl_setopt($curl, CURLOPT_SSLKEY, 'rsa.key');
$output = curl_exec($curl);
if($output === false) {
throw new Exception(sprintf("%s[%s]",curl_errno($curl), curl_error($curl)));
}
curl_close($curl);
return $output;
}
curl_post_file_http
$data[‘file’] = uploadFile($_FILES[‘tmp_name’],$_FILES[‘type’],$_FILES[‘name’]);
/**
* 发送
* @param string $url 请求地址
* @param array $data 请求数据
* @return int|mixed
*/
function curl_post_file_http($url, $data)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
$output = curl_exec($curl);
if ($output === false) {
throw new Exception(sprintf("%s[%s]",curl_errno($curl), curl_error($curl)));
}
curl_close($ch);
return $output;
}
//使用示例
$curl_post_file_data['param'] = $param;
$curl_post_file_data['guarantee_pdf'] = new CURLFile($local_file_path);//文件必须在本地
$curl_post_file_ret = curl_post_file($url, $curl_post_file_data);
curl_post_files_http
$data['files'] = [
uploadFile($_FILES[0]['tmp_name'],$_FILES[0]['type'],$_FILES[0]['name']);
uploadFile($_FILES[1]['tmp_name'],$_FILES[1]['type'],$_FILES[1]['name']);
]
//请求方法用 curl_post_file($url, $data)
soap
$soapClient = new \SoapClient(null,[
'location' => 'url',
'uri' => 'uri',
]);
$ret = $soapClient->__soapCall('function_name',['param' => $param]);