I have not much experience with cUrl. I am creating a bot which login automatically to a site. I have used cUrl to do this. when i run my script it logs me in successfully ( I get the response "Ok" ) but when i redirects to some other page I get logged out. Here is my code:
<?php
$username="username";
$userpass="password";
$url="https://www.invertironline.com/User/DoLogin";
$cookie="cookie.txt";
$postdata = "username=".$username."&password=".$userpass."";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64;
x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, realpath($cookie));
curl_setopt ($ch, CURLOPT_COOKIEFILE, realpath($cookie));
curl_setopt ($ch, CURLOPT_REFERER, $url);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = 'application/xhtml+voice+xml;version=1.2, application/x-
xhtml+voice+xml;version=1.2, text/html, application/xml;q=0.9,
application/xhtml+xml, image/png, image/jpeg, image/gif, image/x-xbitmap,
*/*;q=0.1';
$headers[] = 'Connection: Keep-Alive';
$headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-
8';
$headers[] = 'X-Requested-With: XMLHttpRequest';
$headers[] = 'Referer: https://www.invertironline.com/mercado/cotizaciones';
$headers[] = 'Origin: https://www.invertironline.com';
$headers[] = 'Host: www.invertironline.com';
curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt ($ch, CURLOPT_HEADER, 1);
$result = curl_exec ($ch);
curl_close($ch);
echo $result;
header("Location: https://www.invertironline.com/mercado/cotizaciones");
here are some additional information: if I create a html form with the username and password with action and submit it, it logs me in and give back the following response :
{"result":"OK","redirect":"/MiCuenta/EstadoCuenta"}
and in case of wrong credentials i get this response:
{"result":"Error"}
so when I use curl I am getting same response but when I try to redirect I am not logged in ( probably session is not saved) I have spend 3 days trying to find a solution for this but still can't solve it Any help will be highly appreciated