It's an old subject, but I'll answer for those who are interested :)
In fact you need to keep your cookies in a file and use them again in your second attempt to login.
Something like that :

$path_cookie = 'connexion_temporaire.txt';
if (!file_exists($path_cookie)) touch($path_cookie); 


$postfields = array(
        'action' =&gt; 'login',
        'format'=&gt; 'json',
        'lgname' =&gt; $login,
        'lgpassword' =&gt; $pass
);

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $lienTestWiki);
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($curl, CURLOPT_COOKIEJAR, $path_cookie); // you put your cookie in the file
$connexion=curl_exec($curl);
$json_connexion = json_decode($connexion, true);
$tokenConnexion=$json_connexion['login']['token']; // you take the token and keep it in a var for your second login

// /!\ don't close the curl conection or initialize a new one or your session id will change !

$postfields = array(
        'action' =&gt; 'login',
        'format'=&gt; 'json',
        'lgtoken' =&gt; $tokenConnexion,
        'lgname' =&gt; $login,
        'lgpassword' =&gt; $pass

);


curl_setopt($curl, CURLOPT_URL, $lienTestWiki);
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($curl, CURLOPT_COOKIEFILE, $path_cookie); //get the previous cookie
$connexionToken=curl_exec($curl);

var_dump($connexionToken); 


When running this you should see a success this time :)

How to login using mediawiki API?
I know several questions were asked about the difference between "going to" and "will".
Based on several answers (see, for instance, here, here and here), I understood that "will" is more spontaneous and "going to" is used with more planned actions.

So, it seems that everything is pretty fine. However, in this question, Kosmonaut has an answer in which he states:
"Let's say that tomorrow you will walk your dog from 7 - 8 AM".

On the one hand, you probably planned to walk your dog long before and thus I should use "Let's say that tomorrow you're going to walk your dog from 7 - 8 am". 

On the other hand, since I'm saying "let's say...", I'm deciding right now (thus, unplanned) that you will walk your dog. So, even though in this hypothetical situation you made a plan, I'm in a more spontaneous mood deciding right now that that's what you will do tomorrow, and, thus, I should use "will".

Which one (if any) of the above explanations is right?
