PHP简单的Curl的Get请求和Curl的Post请求和file_get_contents的Get请求获取接口JSON数据

180it 2020-10-20 PM 1381℃ 0条

PHP携带Cookie用Curl进行Post或Get请求获取数据
简单的curl请求(Get请求)

<?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请求)

<?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请求)

<?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请求)

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

来源:https://www.vvhan.com/curlpostfileget.html

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

PHP简单的Curl的Get请求和Curl的Post请求和file_get_contents的Get请求获取接口JSON数据