方法1(多用于get请求)
file_get_contents();
方法2(不带header头的普通请求)
function curl_get_contents($url = '', $ispost = 0, $post_data = array())
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($ispost)
{
curl_setopt($ch, CURLOPT_POST, $ispost);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
方法3(header头携带自定义参数)
function http_request($url1,$data = null)
{
$curl= curl_init();
curl_setopt($curl,CURLOPT_URL,$url1);
curl_setopt ($curl,CURLOPT_HTTPHEADER,array (
'outid: 0000000000000000'
) );
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,FALSE);
if (!empty($data)){
curl_setopt($curl,CURLOPT_POST,1);
curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
}
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
$output= curl_exec($curl);
curl_close($curl);
return $output;
}
方法4(携带自定义cookie)
function https_request($url, $data = null,$cookie)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
$header[] = 'cookiew:'.$cookie;
curl_setopt($curl, CURLOPT_HTTPHEADER,$header);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
方法5(自定义header头信息)
<?php
function curlRequest($url,$params = array(),$header=array())
{
$ch= curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch,CURLOPT_HEADER,1);
curl_setopt($ch,CURLINFO_HEADER_OUT,true);
curl_setopt($ch,CURLOPT_ENCODING,'gzip,deflate');
curl_setopt($ch,CURLOPT_TIMEOUT,5);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$params);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
curl_setopt($ch,CURLOPT_HEADERFUNCTION,
function ($curl,$header)use (&$headers) {
$len= strlen($header);
$header = explode(':',$header,2);
if (count($header)< 2)
return $len;
$headers[strtolower(trim($header[0]))]= trim($header[1]);
return $len;
}
);
$res= array();
$res['data']= curl_exec($ch);
$res['response_header']= $headers;
$request_header= curl_getinfo($ch,CURLINFO_HEADER_OUT);
$res['request_header']= $request_header;
$errno= curl_errno($ch);
if ($errno) {
echo 'Errno:'.$errno.'<br>error:'.curl_error($ch).'<br>';
}
curl_close($ch);
return $res;
}
$url= "www.baidu.com";
$content['username']= "admin";
$content['password']= "123456";
$header= array(
"Cache-Control: no-cache",
"Connection: keep-alive",
"errorlog: true"
);
$data= curlRequest($url,$content,$header);
print_r($data);