WebLinuxStudy

导航

 
<?php
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)
{
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        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,
);
}
posted on 2023-09-21 11:09  WebLinuxStudy  阅读(63)  评论(0编辑  收藏  举报