我尝试使用PHP和CURL从网页上获取Cookie文件:https://www.receita.fazenda.gov.br/Aplicacoes/SSL/ATCTA/CPF/ConsultaSituacao/ConsultaPublica.asp
但是在我第一次不能接受它之后,只需1º连接就可以在“响应标题”中使用cookie,在连接1º之后它不会更多。
我尝试发送CURL请求清理缓存,或者避免缓存,但我没有成功。
因此,当我尝试在浏览器中执行此操作时,我注意到的是,我一直在清理缓存,我可以在“响应标题”中看到cookie,我怎样才能使用PHP + CURL,我需要获取cookie?
谢谢!
清理缓存浏览器后,请求1º请求CODE:
define('COOKIELOCAL', realpath('./').'/');
$ch = curl_init("https://www.receita.fazenda.gov.br/Aplicacoes/SSL/ATCTA/CPF/ConsultaPublica.asp");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
/* trying to clean the cache */
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Pragma: no-cache",
"Cache-control: no-cache"
));
$result = curl_exec($ch);
echo 'Cookie...........:' . file_get_contents($cookieFile); // Empty cookie file here until now
答案 0 :(得分:0)
这应该有效
<?php
declare(strict_types=1);
$ch=curl_init('https://www.receita.fazenda.gov.br/Aplicacoes/SSL/ATCTA/CPF/ConsultaSituacao/ConsultaPublica.asp');
$cookiefile=tmpfile();
$cookiefilepath=stream_get_meta_data($cookiefile)['uri'];
curl_setopt_array($ch, array(
CURLOPT_COOKIEFILE=>$cookiefilepath,
CURLOPT_COOKIEJAR=>$cookiefilepath,
CURLOPT_SSL_VERIFYPEER=>false,
CURLOPT_SSL_VERIFYHOST=>false
));
curl_exec($ch);
curl_close($ch);
$cookies=file_get_contents($cookiefilepath);
var_dump($cookies);