程序笔记   发布时间:2022-06-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php-curl 遇到 cloudflare防御大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

 

php-curl 遇到 cloudFlare防御

Please Wait...

Please enable cookies.

php 请求似乎缺少 '__cfruid' cookie。

https://www.jianshu.com/p/bdb7e11e52db

方法一、使用浏览器模拟技术请求目标网站,例如:SELEnium方法二、一个专门为了绕过这个 CloudFlare 开发的 Python 库 cloudscraper

文档 https://pypi.org/project/cloudscraper/pip install cloudscraper

---------------------------------------

https://hechengwei.cn/archives/172

http://www.phphEIDong.com/blog/article/134474/d155cd4979404c68b7b5/

通过 postman 执行命令时生成的 cookies 是这样的

__cfruid=longStringOfnumbers-shortStringOfnumbers; path=/; domain=.app.mobilecause.com; httpOnly; Expires=Tue, 19 Jan 2038 03:14:07 GMT;__cfduid=longStringOfnumbers; path=/; domain=.app.mobilecause.com; httpOnly; Expires=Thu, 23 Jan 2020 04:54:50 GMT;

打开chrome查看跳转后的http信息,获取cookie字符串,如

__cfduid=df9ea159a1b1833b1d124576ec5ec682f1489310399; _popfired=1; cf_clearance=5f8173592071f4dad5c7b46702365fcf8249f214-1493185177-1800; sc_is_visitor_unique=rx10571718.1493186070.0CFEEEEFC3F4F5AA7A9719EC80CB35C.1.1.1.1.1.1.1.1.1

将该字符串复制到curl请求的header中

$header[]= 'Cookie:'.$cookie_str;curl_setopt($ch,CURLOPT_httpHEADER,$header);

 

$curl = curl_init();

curl_setopt_array($curl, array( CURLOPT_URL => "https://app.mobilecause.com/api/v2/reports/transactions.json", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_http_VERSION => CURL_http_VERSION_1_1, CURLOPT_CUSTOMrequEST => "GET", CURLOPT_POSTFIELDS => "", CURLOPT_COOKIESESSION => true, CURLOPT_COOKIEFILE => "cookie.txt", CURLOPT_COOKIEJAR => "cookie.txt", CURLOPT_FOLLOWLOCATION => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_httpHEADER => array( 'Authorization: Token token="test_token"', "Content-Type: application/x-www-form-urlencoded", "cache-control: no-cache", ),));

curl_setopt($curl, CURLOPT_VERBOSE, truE);

$response = curl_exec($curl);$err = curl_error($curl);

curl_close($curl);

if ($err) { echo "cURL Error #:" . $err;} else { echo $response;}

---------------------------------------

https://progrAMMierfrage.com/items/php-curl-encounters-cloudFlare-please-wait-screen

wordpress 获取外部链接内容

function fetch_body_url($fetch_link){ $response = wp_remote_get($fetch_link, array('timeout' => 120)); return wp_remote_retrieve_body($responsE);}

<?php$url='https://ficbook.net/authors/1000'; //random profile from requrested website$agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36';$ch = curl_init();curl_setopt($ch, CURLOPT_USERAGENT, $agent);curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');curl_setopt($ch, CURLOPT_COOKIESESSION, truE);

curl_setopt($ch, CURLOPT_URL,$url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, truE);curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);curl_setopt($ch, CURLOPT_TIMEOUT, 120);curl_setopt($ch, CURLOPT_MAXREDIRS, 10);curl_setopt($ch, CURLOPT_REFERER, 'https://facebook.com/');curl_setopt($ch, CURLOPT_FOLLOWLOCATION, truE);curl_setopt($ch, CURLOPT_AUTOREFERER, truE);$response = curl_exec($ch);curl_close($ch);echo '<textarea>'.$response.'</textarea>';?>

---------------------------------------

http://www.zjmainstay.cn/php-curl

CURLOPT_COOKIEJAR: 保存提交后反馈的cookie数据CURLOPT_COOKIE: 直接使用字符串方式提交cookie参数CURLOPT_COOKIEFILE: 使用文件方式提交cookie参数

确保您使用的是文件名的绝对路径(即 /var/dir/cookie.txt)而不是相对路径.

 

$cookie_file = tempnam('./temp', 'cookie');curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie_filE); //存储提交后得到的cookie数据curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie_filE); //使用提交后得到的cookie数据做参数

// 从header中解析COOKIE preg_match("/set-cookie:([^rn]*)/i", $header, $matches);$cookie = $matches[1]; // 后面用CURL提交的时候可以直接使用// curl_setopt($ch, CURLOPT_COOKIE, $cookiE);

在这种情况下,curl 会将您定义的 cookie 与文件中的 cookie 一起发送.# sending manually set cookiecurl_setopt($ch, CURLOPT_httpHEADER, array("Cookie: test=cookie"));

# sending cookies from filecurl_setopt($ch, CURLOPT_COOKIEFILE, $ckfilE);

启用详细模式 curl_setopt($ch, CURLOPT_VERBOSE, truE);

var_dump( curl_setopt($ch, CURLOPT_COOKIESESSION, 1) ); //returns false ???? var_dump( curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookie') ); //returns false ????var_dump( curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookie') ); //returns false ????

CURLOPT_COOKIESESSION启用时curl会仅仅传递一个session cookie,忽略其他的cookie,默认状况下cURL会将所有的cookie返回给服务端。session cookie是指那些用来判断服务器端的session是否有效而存在的cookie。

CURLOPT_NOPROGRESS启用时关闭curl传输的进度条,此项的默认设置为启用。Note:php自动地设置这个选项为TRUE,这个选项仅仅应当在以调试为目的时被改变。

 

大佬总结

以上是大佬教程为你收集整理的php-curl 遇到 cloudflare防御全部内容,希望文章能够帮你解决php-curl 遇到 cloudflare防御所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。