//getcurl get读取数据
function curlGet($url){
$UserAgent = ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36‘;
$this_header = array("content-type: application/x-www-form-urlencoded; charset=UTF-8");
$curl = curl_init(); //创建一个新的CURL资源
curl_setopt($curl,CURLOPT_HTTPHEADER,$this_header);
curl_setopt($curl, CURLOPT_URL, $url); //设置URL和相应的选项
curl_setopt($curl, CURLOPT_HEADER, 0); //0表示不输出Header,1表示输出
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //设定是否显示头信息,1显示,0不显示。
//如果成功只将结果返回,不自动输出任何内容。如果失败返回FALSE
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_ENCODING, ‘‘); //设置编码格式,为空表示支持所有格式的编码
//header中“Accept-Encoding: ”部分的内容,支持的编码格式为:"identity","deflate","gzip"。
curl_setopt($curl, CURLOPT_USERAGENT, $UserAgent);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
//设置这个选项为一个非零值(象 “Location: “)的头,服务器会把它当做HTTP头的一部分发送(注意这是递归的,PHP将发送形如 “Location: “的头)。
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
//curlPost post读取数据
function curlPost($url, $data, $timeout = 30)
{
$ssl = substr($url, 0, 8) == "https://" ? TRUE : FALSE;
$ch = curl_init();
$opt = array(
CURLOPT_URL => $url,
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_POSTFIELDS => (array)$data,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => $timeout,
);
if ($ssl)
{
$opt[CURLOPT_SSL_VERIFYHOST] = FALSE;
$opt[CURLOPT_SSL_VERIFYPEER] = FALSE;
}
curl_setopt_array($ch, $opt);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}