PHP携带Cookie用Curl进行Post或Get请求获取数据

#PHP全能Curl请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* curl发送HTTP请求方法
* @param $url
* @param string $method
* @param array $params
* @param array $header
* @param int $timeout
* @param bool|false $multi
* @return mixed
* @throws Exception
*/
function hansCurl( $url, $method = 'GET', $params = array(), $header = array(),
$timeout = 30, $multi = false ) {
$curl = curl_init();
curl_setopt( $curl, CURLOPT_TIMEOUT, $timeout );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt( $curl, CURLOPT_HTTPHEADER, $header );
switch ( strtoupper( $method ) ) {
case 'GET':
if ( !empty( $params ) ) {
$uri = parse_url( $url );
$url .= ( empty( $uri[ 'query' ] ) ? '?' : '&' ) . http_build_query( $params );
}
curl_setopt( $curl, CURLOPT_URL, $url );
break;
case 'POST':
curl_setopt( $curl, CURLOPT_URL, $url );
curl_setopt( $curl, CURLOPT_POST, true );
$params = $multi ? $params : http_build_query( $params ); //判断是否传输文件
curl_setopt( $curl, CURLOPT_POSTFIELDS, $params );
break;
default:
throw new Exception( '不支持的请求方式!' );
}
$response = curl_exec( $curl );
$error = curl_error( $curl );
curl_close( $curl );
if ( $error ) {
throw new Exception( '请求发生错误:' . $error );
}
return $response;
}

简单的curl请求(Get请求)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
function hansCurl($url)
{
$url="https://www.vvhan.com";
$ip = rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255);
$header[] = "accept: application/json";
$header[] = "accept-encoding: gzip, deflate";
$header[] = "accept-language: en-US,en;q=0.8";
$header[] = "content-type: application/json";
$header[] = "CLIENT-IP:" . $ip;
$header[] = "X-FORWARDED-FOR:" . $ip;
$cookie = "cookie";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); //设置传输的 url
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //发送 http 报头
curl_setopt($ch, CURLOPT_COOKIE, $cookie); //设置Cookie
curl_setopt($ch, CURLOPT_REFERER, "https://www.vvhan.com"); //设置Referer
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36"); //设置UA
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); // 解码压缩文件
//curl_setopt($ch, CURLOPT_COOKIEJAR, "./cookie/cookie.txt");//保存cookie文件
//curl_setopt($ch, CURLOPT_COOKIEFILE, "./cookie/cookie.txt"); //调用cookie文件
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // 设置超时限制防止死循环
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
?>

简单的curl请求(Post请求)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
function hansCurl($url)
{
$url="https://www.vvhan.com";
$ip = rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255);
$header[] = "accept: application/json";
$header[] = "accept-encoding: gzip, deflate";
$header[] = "accept-language: en-US,en;q=0.8";
$header[] = "content-type: application/json";
$header[] = "CLIENT-IP:" . $ip;
$header[] = "X-FORWARDED-FOR:" . $ip;
$cookie="cookie";
$post_data = array(
"token" => "123456"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);//设置传输的 url
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //发送 http 报头
curl_setopt($ch, CURLOPT_COOKIE, $cookie);//设置Cookie
curl_setopt($ch, CURLOPT_REFERER, "https://www.vvhan.com");//设置Referer
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36");//设置UA
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); // 解码压缩文件
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);// 对认证证书来源的检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);// 从证书中检查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // 设置超时限制防止死循环
curl_setopt($ch, CURLOPT_POST, 1); //设置POST发送数据
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);//发送POST数据内容
//curl_setopt($ch, CURLOPT_COOKIEJAR, "./cookie/cookie.txt");//保存cookie文件
//curl_setopt($ch, CURLOPT_COOKIEFILE, "./cookie/cookie.txt"); //调用cookie文件
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
?>

#PHP的curl上传文件(Post请求)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
function hansCurl($url)
{
$url = "https://www.vvhan.com";
$names="1.png";
$ip = rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255);
$header[] = "accept: application/json";
$header[] = "accept-encoding: gzip, deflate";
$header[] = "accept-language: en-US,en;q=0.8";
$header[] = "content-type: application/json";
$header[] = "CLIENT-IP:" . $ip;
$header[] = "X-FORWARDED-FOR:" . $ip;
$cookie = "cookie";
$post_data = [
'name' => $names,
'attrFile' => new CURLFile(realpath('hanCURLFile/' . $names)),
];
$ch = curl_init(); // 启动一个CURL会话
curl_setopt($ch, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'); // 模拟用户使用的浏览器
curl_setopt($ch, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($ch, CURLOPT_REFERER, 'https://www.vvhan.com'); // 自动设置Referer
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); // Post提交的数据包
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // 设置超时限制防止死循环
curl_setopt($ch, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
//curl_setopt($ch, CURLOPT_COOKIEJAR, "./cookie/cookie.txt");//保存cookie文件
//curl_setopt($ch, CURLOPT_COOKIEFILE, "./cookie/cookie.txt"); //调用cookie文件
$output = curl_exec($ch); // 执行操作
curl_close($ch); // 关闭CURL会话
return ($output);
}
?>

简单的file_get_contents请求(Get请求)


1
2
3
4
5
<?php
$url = 'https://www.vvhan.com/';
$data = file_get_contents($url);
exit($data);
?>