GET案例
function curl_get($url, $param = null, $options = null){
$defaultOptions = array(
'timeout' => 30,
'header' => [],
'get_header' => false,
'cookie' => '',
'cookie_file' => '',
'followlocation' => 0,
'ssl' => 0,
'referer' => null
);
foreach ($defaultOptions as $key => $item) {
if(!isset($options[$key])){
$options[$key] = $item;
}
}
$result = array(
'code' => 0,
'msg' => 'success',
'body' => ''
);
if (is_array($param)) {
$param = http_build_query($param);
}
$url = strstr($url,'?')? trim($url,'&').'&'.$param: $url.'?'.$param;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
!empty($options['timeout']) && curl_setopt($ch, CURLOPT_TIMEOUT, $options['timeout']);
if (!empty($options['header'])) {
if (is_string($options['header'])) {
$header = str_replace(array("
", "
", "
"), "__SLONG__", $options['header']);
$header = explode('__SLONG__', $header);
$options['header'] = array_values(array_filter($header));
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $options['header']);
}
if (!empty($options['cookie_file']) && file_exists($options['cookie_file'])) {
curl_setopt($ch, CURLOPT_COOKIEFILE, $options['cookie_file']);
curl_setopt($ch, CURLOPT_COOKIEJAR, $options['cookie_file']);
} elseif (!empty($options['cookie'])) {
curl_setopt($ch, CURLOPT_COOKIE, $options['cookie']);
}
!empty($options['followlocation']) && curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $options['followlocation']);
empty($options['ssl']) && curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $options['ssl']);
!empty($options['referer']) && curl_setopt($ch, CURLOPT_REFERER, $options['referer']);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if (count($options) != count($defaultOptions)) {
foreach ($options as $key => $item) {
if (!isset($defaultOptions[$key])) {
curl_setopt($ch, $key, $item);
}
}
}
$output = curl_exec($ch);
if ($output === false) {
$result['code'] = 1;
$result['msg'] = "CURL Error:".curl_error($ch);
} else {
if ($options['get_header']) {
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$result['header'] = substr($output, 0, $headerSize);
$result['body'] = substr($output, $headerSize);
}else{
$result['body'] = $output;
}
}
curl_close($ch);
return $result;
}
POST案例
function curl_post($url, $param = null, $options = array()){
$defaultOptions = array(
'timeout' => 30,
'header' => [],
'get_header' => false,
'cookie' => '',
'cookie_file' => '',
'followlocation' => 0,
'ssl' => 0,
'referer' => null
);
foreach ($defaultOptions as $key => $item) {
if (!isset($options[$key])) {
$options[$key] = $item;
}
}
$result = array(
'code' => 0,
'msg' => 'success',
'body' => ''
);
if (is_array($param)) {
$param = http_build_query($param);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
!empty($options['timeout']) && curl_setopt($ch, CURLOPT_TIMEOUT, $options['timeout']);
if (!empty($options['header'])) {
if (is_string($options['header'])) {
$header = str_replace(array("
", "
", "
"), "__SLONG__", $options['header']);
$header = explode('__SLONG__', $header);
$options['header'] = array_values(array_filter($header));
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $options['header']);
}
if (!empty($options['cookie_file']) && file_exists($options['cookie_file'])) {
curl_setopt($ch, CURLOPT_COOKIEFILE, $options['cookie_file']);
curl_setopt($ch, CURLOPT_COOKIEJAR, $options['cookie_file']);
} elseif (!empty($options['cookie'])) {
curl_setopt($ch, CURLOPT_COOKIE, $options['cookie']);
}
!empty($options['followlocation']) && curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $options['followlocation']);
empty($options['ssl']) && curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $options['ssl']);
!empty($options['referer']) && curl_setopt($ch, CURLOPT_REFERER, $options['referer']);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if (count($options) != count($defaultOptions)) {
foreach ($options as $key => $item) {
if (!isset($defaultOptions[$key])) {
curl_setopt($ch, $key, $item);
}
}
}
$output = curl_exec($ch);
if ($output === false) {
$result['code'] = 1;
$result['msg'] = "CURL Error:".curl_error($ch);
} else {
if ($options['get_header']) {
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$result['header'] = substr($output, 0, $headerSize);
$result['body'] = substr($output, $headerSize);
} else {
$result['body'] = $output;
}
}
curl_close($ch);
return $result;
}
|