Php curl中的Cookies和隐藏安全值

Php curl中的Cookies和隐藏安全值,php,curl,cookies,Php,Curl,Cookies,我试图使用PHP/curl登录到一个站点,以便能够访问仅对站点上的注册用户可用的数据。表单请求电子邮件、密码和隐藏的安全值。安全值随每次登录页面加载随机生成。我认为这是造成我的问题的原因:在下面的脚本中,curl首先请求页面,代码解析安全值,然后curl尝试登录。但是,正如我使用print\r验证的那样,第二次页面加载时,安全代码将发生更改 代码如下: $email = urlencode("emailhandleIuse"); $password = "myPassword"; $loginU

我试图使用PHP/curl登录到一个站点,以便能够访问仅对站点上的注册用户可用的数据。表单请求电子邮件、密码和隐藏的安全值。安全值随每次登录页面加载随机生成。我认为这是造成我的问题的原因:在下面的脚本中,curl首先请求页面,代码解析安全值,然后curl尝试登录。但是,正如我使用print\r验证的那样,第二次页面加载时,安全代码将发生更改

代码如下:

$email = urlencode("emailhandleIuse");
$password = "myPassword";
$loginURL = "https://www.example.com/login/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $loginURL);
$ch = setCurlParameters($ch);
$securityCode = curl_exec($ch);
$securityCode = urlencode(scrape_between($securityCode, "<input type=\"hidden\" name=\"security\" value=\"","\" />")); // gets security value from the page HTML
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'email='.$email.'&password='.$password.'&security='.$securityCode);
curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, "https://www.example.com/followingpage");
$secondPage = curl_exec($ch);
curl_close($ch);

function setCurlParameters($ch) {
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17");
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
    curl_setopt($ch, CURLOPT_TIMEOUT, 120);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
    curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
    curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, TRUE);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    return $ch;
}

function scrape_between($data, $start, $end){   
    $data = stristr($data, $start); // Stripping all data from before $start
    $data = substr($data, strlen($start));  // Stripping $start
    $stop = stripos($data, $end);   // Getting the position of the $end of the data to scrape
    $data = substr($data, 0, $stop);    // Stripping all data from after and including the $end of the data to scrape
    return $data;   // Returning the scraped data from the function
    }
$email=urlencode(“emailhandleIuse”);
$password=“myPassword”;
$loginURL=”https://www.example.com/login/";
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$loginURL);
$ch=设置参数($ch);
$securityCode=curl\u exec($ch);
$securityCode=urlencode(在($securityCode,“”)之间进行刮除);//从HTML页面获取安全值
卷曲设置($ch,卷曲设置桩,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,'email='.$email.&password='.$password.&security='.$securityCode);
curl_exec($ch);
curl_setopt($ch,CURLOPT_URL,”https://www.example.com/followingpage");
$secondPage=curl\u exec($ch);
卷曲关闭($ch);
函数设置参数($ch){
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,TRUE);
curl_setopt($ch,CURLOPT_AUTOREFERER,TRUE);
curl_setopt($ch,CURLOPT_USERAGENT,“Mozilla/5.0(Windows NT 6.1;WOW64)AppleWebKit/537.17(KHTML,像Gecko)Chrome/24.0.1312.52 Safari/537.17”);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,120);
curl_setopt($ch,CURLOPT_超时,120);
curl_setopt($ch,CURLOPT_MAXREDIRS,10);
curl_setopt($ch,CURLOPT_COOKIEJAR,'cookies.txt');
curl_setopt($ch,CURLOPT_COOKIEFILE,'cookies.txt');
curl_setopt($ch,CURLOPT_COOKIESESSION,TRUE);
curl_setopt($ch,CURLOPT_UNRESTRICTED_AUTH,TRUE);
curl_setopt($ch,CURLOPT_VERBOSE,0);
返回$ch;
}
函数在($data,$start,$end){
$data=stristr($data,$start);//从$start之前剥离所有数据
$data=substr($data,strlen($start));//剥离$start
$stop=stripos($data,$end);//获取要刮取的数据$end的位置
$data=substr($data,0,$stop);//从后面剥离所有数据,包括要刮取的数据的$end
return$data;//从函数返回刮取的数据
}
$secondPage最终成为一个登录页面,因为我在发布我认为需要的详细信息后没有进行验证


非常感谢您的帮助。

完成第一个请求后,您必须关闭curl句柄,否则cookie将不会存储到文件中。这就是为什么您的第二个请求(登录)没有将cookie转发到服务器

curl_close($ch);
对于cookie文件,最好在从服务器运行时使用完整路径
c:/tmp/cookie.txt
(我认为)

顺便说一下,使用详细模式,您可以看到自己实际发生了什么

curl_setopt($ch, CURLOPT_VERBOSE, TRUE);

执行第一个请求后,必须关闭curl句柄,否则cookie将不会存储到文件中。这就是为什么您的第二个请求(登录)没有将cookie转发到服务器

curl_close($ch);
对于cookie文件,最好在从服务器运行时使用完整路径
c:/tmp/cookie.txt
(我认为)

顺便说一下,使用详细模式,您可以看到自己实际发生了什么

curl_setopt($ch, CURLOPT_VERBOSE, TRUE);