duanfei1987 2019-05-02 00:23
浏览 61

使用PHP cURL从外部系统搜索中获取结果

I'm trying to create a routine to extract the results of a search on an external system using cURL in PHP. I have already copied the data from the Request Headers and with each connection I retrieve the cookie and the javax.faces.ViewState, but the cURL does not return any search results. I have modified the code in several ways, but none works. If I do the search for the system itself and copy the cookie and 'javax.faces.ViewState' to my code it works, but when I try to get the cookie and 'javax.faces.ViewState' through cURL it does not return any results. I need a light because it's been 3 days since I've been trying to make this query.

NOTE: In the example the second request manually copied the cookie and the 'javax.faces.ViewState' that is generated on the first request.

CODE:

// 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;
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算