Authentication failed due to inconsistency between token and cookie (s) .
When loading a page to receive a token with get_token() server sends you a cookie that you do not save.
Later, when you try to log in, the server expects to receive the same cookie that it sent to you when you received the token . But you do not send it.
I suggest you rewrite get_token() with curl and save cookies in cookie.txt . This will allow you to pass them later when you call login()
Like this:
function get_token() { $url = 'https://www.foo.com/'; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_COOKIEJAR, "cookie.txt"); curl_setopt($curl, CURLOPT_COOKIEFILE, "cookie.txt"); curl_setopt($curl, CURLOPT_TIMEOUT, 40000); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); $result = curl_exec($curl); curl_close($curl); unset($curl); $html = str_get_html( $result ); $token = ""; foreach($html->find('input') as $element) { if($element->name == "token") { $token = $element->value; } } if (!$token) { die('No token found'); } return $token; }
Important:
Delete
$fp = fopen("cookie.txt", "w"); fclose($fp);
From login() , as this truncates the cookie.txt to zero byte, and you do not want to delete the cookie you just received.
Note that curl_exec() creates a specified cookie if it does not exist.
Paolo
source share