php json获取get请求,PHP简单的Curl的Get请求和Curl的Post请求和file_get_contents的Get请求获取接口JSON数据...

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

简单的curl请求(Get请求)

function hansCurl($url)

{

$url="https://www.yyob.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请求)

function hansCurl($url)

{

$url="https://www.yyob.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请求)

function hansCurl($url)

{

$url = "https://www.yyob.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请求)

$url = 'https://www.yyob.com/';

$data = file_get_contents($url);

exit($data);

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值