<?php
$url = 'http://abc.com';
$method = 'get';
$optionArr = array();
$optionArr['cookie'] = 'userid=1641281255339_h8wgml1013;';
$result = curlDeal($url, $method, array(), $optionArr);
print_r($result);
die;
/**
* CURL
* @param string $url 网址
* @param string $method GET/POST
* @param array $dataArr 参数
* @param array $optionArr 选项 timeout表示最大执行时间,cookie表示cookie信息
* @return array
*/
function curlDeal($url, $method, $dataArr = array(), $optionArr = array())
{
$method = strtolower($method);
//GET方式
if ('get' == $method) {
$url = $url . '?' . http_build_query($dataArr);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
//设置返回信息中是否包含响应信息头,true表示输出信息头,false表示不输出
curl_setopt($ch, CURLOPT_HEADER, false);
//设置获取的信息,true表示以文件流的形式返回,false表示直接输出
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//HTTPS
if (stripos($url, "https://") !== FALSE) {
//规避SSL验证
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//跳过HOST验证
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
//POST方式
if ('post' == $method) {
curl_setopt($ch, CURLOPT_POST, true);
if(is_array($dataArr) && !empty($dataArr)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($dataArr));
}
}
//设置cookie
if (isset($optionArr['cookie']) && $optionArr['cookie']) {
$headers = array();
$headers[] = "Cookie: " . $optionArr['cookie'];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
//设置最长执行时间
if (isset($optionArr['timeout']) && $optionArr['timeout'] > 0) {
curl_setopt($ch, CURLOPT_TIMEOUT, $optionArr['timeout']);
}
$content = curl_exec($ch);
//输出请求状态码
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return array(
'statusCode' => $statusCode,
'content' => $content
);
}
标签:ch,optionArr,array,获取数据,cookie,CURL,curl,setopt From: https://www.cnblogs.com/WebLinuxStudy/p/16976842.html