使用PHP和cURL在loggin之后保持Session活动

时间:2016-02-15 16:43:05

标签: php curl cookies

我需要在登录后收集站点中的一些数据。我使用POST提交了初始表单数据,还有一些需要隐藏的输入字段。这很好,给我看下一页是另一种形式。当我尝试使用POST提交第二个表单数据时,该页面始终显示该消息: “VelocityServlet:处理模板时出错”。也许有任何问题可以使会话保持活跃状态​​。有人可以帮帮我吗?

按照我正在使用的代码:

(显然隐藏了一些信息)

<?php

// LOGIN FORM FIELD
$url = "https://<url_site>/LoginModuloConvenio?acao=login";

// PROXY
$proxy = 'proxy.XXXXXXX';

// INPUT FIELDS
$fields = array(

    'lojaCodigo' => 'xxxxxx',                           // Number of 6 digits
    'login'      => 'xxxxxxxxxxxxxx',                   // Number of 14 digits 
    'senha'      => 'xxxxx',                            // Password
    'button'     => 'Entrar',
    'nomeLoja'   => 'xxxxx'                             // Name

);

$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR,  'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 10);

$result = curl_exec($ch);
echo $result;


// /////////////////////////////
// SECOND FORM
// /////////////////////////////

$fields2 = array(
    'version'            => '',
    'codigoBean'         => '',
    'codigo'             => '503120',
    'isSemMenu'          => 'N',
    'codigoFiltro'       => '',
    'nomeFiltro'         => 'Roger Bastida',
    'numeroCartaoFiltro' => '',
    'cpfFiltro'          => '',
    'rgFiltro'           => '',
    'estadoFiltro'       => '',
    'campanha'           => '',
    'botao2'             => 'Filtrar'
);

define('HOME' , dirname(__FILE__));

$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_PROXY, $proxy);
curl_setopt($ch2, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
curl_setopt($ch2, CURLOPT_COOKIESESSION, true);
curl_setopt($ch2, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch2, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch2, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch2, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)');
curl_setopt($ch2, CURLOPT_URL, $url);
curl_setopt($ch2, CURLOPT_POST, 1);
curl_setopt($ch2, CURLOPT_POSTFIELDS, $fields2);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, 10);
curl_setopt($ch2, CURLOPT_TIMEOUT, 5);
curl_setopt($ch2, CURLOPT_HEADER, 1);
//curl_setopt($ch2, CURLINFO_HEADER_OUT, 0);
$result2 = curl_exec($ch2);

echo $result2;  
curl_close($ch2);

1 个答案:

答案 0 :(得分:0)

你的两个curl设置之间存在很多不一致,特别是CURLOPT_PROXYTYPE存在于第二个代码块(设置为CURLPROXY_SOCKS5)但不存在第一个(默认为CURLPROXY_HTTP) 。此外,您仍在发送第二个块中的登录URL,但这可能是复制/粘贴错误。

但是,您的主要问题是,CURLOPT_COOKIEJAR指定了一个用于保存Cookie的文件,但在curl_close()调用之后才会that file is not writtenCURLOPT_COOKIEFILE指定要从中读取Cookie的文件。但是,它将采用空字符串并在会话中启用cookie而无需任何配置,那么为什么不重用您的curl对象?您需要更改的是第二次运行的URL和POST字段。

最后,不要 CURLOPT_SSL_VERIFYPEER设为false。这是对灾难的邀请,没有任何借口。如果您无法弄清楚如何在安装中添加适当的证书包,请放下键盘并获得不同的工作!

<?php
$proxy = 'proxy.XXXXXXX';

$login_url = "https://<url_site>/LoginModuloConvenio?acao=login";
$login_fields = array(
    'lojaCodigo' => 'xxxxxx',
    'login'      => 'xxxxxxxxxxxxxx',
    'senha'      => 'xxxxx',
    'button'     => 'Entrar',
    'nomeLoja'   => 'xxxxx',
);

$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_COOKIESESSION  => true,
    CURLOPT_COOKIEFILE     => "",
    CURLOPT_USERAGENT      => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
// hey, this is very bad
//    CURLOPT_SSL_VERIFYPEER => false,
// set this as needed
//    CURLOPT_PROXYTYPE      => CURLPROXY_SOCKS5,
    CURLOPT_PROXY          => $proxy,
    CURLOPT_URL            => $login_url,
    CURLOPT_POST           => true,
    CURLOPT_POSTFIELDS     => $login_fields,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT        => 5,
    CURLOPT_CONNECTTIMEOUT => 5,
    CURLOPT_HEADER         => 1,
    CURLOPT_FOLLOWLOCATION => 10,
));
$result = curl_exec($ch);
echo $result;


// /////////////////////////////
// SECOND FORM
// /////////////////////////////

//what's the second URL?
$submission_url = "???";

$submission_fields = array(
    'version'            => '',
    'codigoBean'         => '',
    'codigo'             => '503120',
    'isSemMenu'          => 'N',
    'codigoFiltro'       => '',
    'nomeFiltro'         => 'Roger Bastida',
    'numeroCartaoFiltro' => '',
    'cpfFiltro'          => '',
    'rgFiltro'           => '',
    'estadoFiltro'       => '',
    'campanha'           => '',
    'botao2'             => 'Filtrar',
);

curl_setopt($ch, CURLOPT_URL, $submission_url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $submission_fields);
$result2 = curl_exec($ch);
echo $result2;

curl_close($ch);