<?php标签:ch,setopt,dataArr,获取数据,curl,php,array,CURLOPT From: https://www.cnblogs.com/WebLinuxStudy/p/17719431.html
header("Content-Type:text/html;charset=UTF-8");
$url = 'https://www.baidu.com';
print_r(curlContent($url));
function curlContent($url, $method = 'get', $dataArr = array(), $headerArr = array())
{
$method = strtolower($method);
$ch = curl_init();
if ('get' == $method)
{
if (is_array($dataArr) && !empty($dataArr))
{
$url .= '?' . http_build_query($dataArr);
}
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 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));
}
}
if (is_array($headerArr) && !empty($headerArr))
{
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr);
}
$response = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return array(
'code' => $statusCode,
'response' => $response,
);
}