我正在尝试创建一个例程,以使用PHP中的cURL提取外部系统上的搜索结果。 我已经从请求标头复制了数据,并且通过每个连接都检索了cookie和javax.faces.ViewState,但是cURL不返回任何搜索结果。 我以几种方式修改了代码,但是没有任何效果。 如果我搜索系统本身并将cookie和'javax.faces.ViewState'复制到我的代码中就可以了,但是当我尝试通过cURL获取cookie和'javax.faces.ViewState'时,它不会返回任何内容。结果。 我需要开灯,因为距尝试进行此查询已经3天了。
注意:在该示例中,第二个请求手动复制了cookie和在第一个请求上生成的'javax.faces.ViewState'。
代码:
// REF
// https://forum.imasters.com.br/topic/442569-problema-curl/
/* ******************************
* Primeira Conexão
* ******************************/
$ch1 = curl_init();
curl_setopt_array($ch1, array
(
CURLOPT_HEADER => true,
CURLOPT_PORT => '8082',
CURLOPT_URL => 'http://centralquadion.ddns.net:8082/MoniWeb/',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT_MS => 20000,
CURLOPT_CUSTOMREQUEST => 'GET',
)
);
$response1 = curl_exec($ch1);
curl_close($ch1);
//Valor do cookie
$pattern_cookie = '/Set-Cookie: JSESSIONID=(.*); Path=\/MoniWeb;/';
$res_cookie = array();
preg_match_all( $pattern_cookie, $response1, $res_cookie );
//Valor do javax.faces.ViewState
$pattern = '/<input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="(.*?)" autocomplete="off" \/>/';
$res_view = array();
preg_match_all( $pattern, $response1, $res_view );
$valor_cookie = $res_cookie[1][0];
$valor_viewstate = $res_view[1][0];
echo $valor_cookie . "<br>";
echo $valor_viewstate . "<br>";
$view_state = $valor_cookie;
$cookie = $valor_viewstate;
/* ******************************
* Segunda Requisição
* ******************************/
$ch2 = curl_init();
$url2 = 'http://centralquadion.ddns.net:8082/MoniWeb/faces/ConsultaOcorrencias.xhtml;jsessionid=' . $cookie;
$Header2 = [
'Accept: application/xml, text/xml, */*; q=0.01',
'Accept-Encoding: gzip, deflate',
'Accept-Language: pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7',
'Connection: keep-alive',
'Content-Length: 752',
'Content-Type: application/x-www-form-urlencoded',
'Cookie: JSESSIONID=' . $cookie,
'Faces-Request: partial/ajax',
'Host: centralquadion.ddns.net:8082',
'Origin: http://centralquadion.ddns.net:8082',
'Referer: http://centralquadion.ddns.net:8082/MoniWeb/',
'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36',
'X-Requested-With: XMLHttpRequest'
];
$PostData2 = [
'consultaOcorrencias' => 'consultaOcorrencias',
'consultaOcorrencias:usuario' => '1111',
'consultaOcorrencias:senha' => '1111',
'consultaOcorrencias:dataInicial_input' => '01/04/2019',
'consultaOcorrencias:dataFinal_input' => '25/04/2019',
'consultaOcorrencias:resultado' => '0',
'consultaOcorrencias:linkArquivoPdf' => '',
'consultaOcorrencias:mensagemErro' => '',
'javax.faces.ViewState' => $view_state,
'javax.faces.partial.ajax' => 'true',
'javax.faces.source' => 'consultaOcorrencias:botaoConsultar',
'javax.faces.partial.execute' => '@all',
'javax.faces.partial.render' => 'consultaOcorrencias:filtro consultaOcorrencias:mensagem consultaOcorrencias:foco consultaOcorrencias:linkArquivoPdf consultaOcorrencias:mensagemErro',
'consultaOcorrencias:botaoConsultar' => 'consultaOcorrencias:botaoConsultar'
];
curl_setopt_array($ch2, array
(
CURLOPT_HEADER => true,
CURLOPT_HTTPHEADER => $Header2,
CURLOPT_PORT => '8082',
CURLOPT_URL => $url2,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT_MS => 20000,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => http_build_query($PostData2),
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_COOKIESESSION => true,
CURLOPT_COOKIE => 'JSESSIONID=' . $cookie,
CURLOPT_ENCODING => 'gzip, deflate'
)
);
//curl_setopt($crl, CURLOPT_COOKIEJAR, dirname(__FILE__).'/cookie.txt');
//curl_setopt($crl, CURLOPT_COOKIEFILE, dirname(__FILE__).'/cookie.txt');
$response2 = curl_exec($ch2);
curl_close($ch2);
/* ******************************
* Terceira Requisição
* ******************************/
$ch3 = curl_init();
$url3 = 'http://centralquadion.ddns.net:8082/MoniWeb/faces/ConsultaOcorrencias.xhtml;jsessionid=' . $cookie;
$Header3 = [
'Accept: application/xml, text/xml, */*; q=0.01',
'Accept-Encoding: gzip, deflate',
'Accept-Language: pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7',
'Connection: keep-alive',
'Content-Length: 602',
'Content-Type: application/x-www-form-urlencoded',
'Cookie: JSESSIONID=' . $cookie,
'Faces-Request: partial/ajax',
'Host: centralquadion.ddns.net:8082',
'Origin: http://centralquadion.ddns.net:8082',
'Referer: http://centralquadion.ddns.net:8082/MoniWeb/',
'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36',
'X-Requested-With: XMLHttpRequest'
];
$PostData3 = [
'consultaOcorrencias' => 'consultaOcorrencias',
'consultaOcorrencias:usuario' => '1111',
'consultaOcorrencias:senha' => '1111',
'consultaOcorrencias:dataInicial_input' => '01/04/2019',
'consultaOcorrencias:dataFinal_input' => '25/04/2019',
'consultaOcorrencias:resultado' => '0',
'consultaOcorrencias:linkArquivoPdf' => '',
'consultaOcorrencias:mensagemErro' => '',
'javax.faces.ViewState' => $view_state,
'javax.faces.partial.ajax' => 'true',
'javax.faces.source' => 'consultaOcorrencias:j_idt59',
'javax.faces.partial.execute' => '@all',
'javax.faces.partial.render' => 'consultaOcorrencias',
'consultaOcorrencias:j_idt59' => 'consultaOcorrencias:j_idt59'
];
curl_setopt_array($ch3, array
(
CURLOPT_HEADER => true,
CURLOPT_HTTPHEADER => $Header3,
CURLOPT_PORT => '8082',
CURLOPT_URL => $url3,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 20000,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => http_build_query($PostData3),
CURLOPT_POSTFIELDS => $post3,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_COOKIESESSION => true,
CURLOPT_COOKIE => 'JSESSIONID='.$cookie.' Path=/MoniWeb',
CURLOPT_ENCODING => 'gzip, deflate'
)
);
$response3 = curl_exec($ch3);
curl_close($ch3);
echo $response1;
echo $response2;
echo $response3;