php cURL cookie未通过

There is a site, to search for phone numbers. I need to make either a php script or a curl command to be able to search from a cron job.

When I visit the search page, a "session" cookie is created, which is used to obtain the results. On the result page, in case the cookie is missing or it contains wrong information, the search does not yield results.

So I figured I visit the search page, to grab the cookie, then post that cookie, alongside the search parameters I need to the result page, which is different (the search page's form action points to that).

The first part is done. I am able to grab the cookie, either parsing the header:

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://www.eofcom.admin.ch/eofcom/public/searchEofcom_InaFree.do');
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_VERBOSE, true);
$result = curl_exec($curl);
curl_close($curl);

preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $result, $matches);
$cookiesStringToPass = '';
$cookies = array();
foreach($matches[1] as $item) {
    parse_str($item, $cookie);
    $cookies = array_merge($cookies, $cookie);
}
$cookiesStringToPass = '';
foreach ($cookies as $name=>$value) {
    if ($cookiesStringToPass) {
         $cookiesStringToPass  .= ';';
    }
    $cookiesStringToPass .= $name . '=' . addslashes($value);
}
// $cookiesStringToPass now contains the cookie names and values 

or storing it in a file using:

curl_setopt($curl, CURLOPT_COOKIEJAR, dirname(__FILE__).'/cookies.txt');
curl_setopt($curl, CURLOPT_COOKIEFILE, dirname(__FILE__).'/cookies.txt');

So far so good. Now in the second part, where I need to submit/post the information to obtain the result (again, it's a different page from the search one), should pass the cookie name/value, which is not happening. Either I set explicitly the cookie in the header, like this:

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://www.eofcom.admin.ch/eofcom/public/listEofcom_InaFree.do");
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    'Cookie: '.$cookiesStringToPass,
    'Content-Type: application/x-www-form-urlencoded'
));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, "nrt=2&pnp=000221&doSearchFreeByNumber=Search");
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION,0);
curl_setopt($curl, CURLOPT_HEADER , 1);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
$result = curl_exec($curl);
curl_close($curl);

or I set the file generated before:

curl_setopt($curl, CURLOPT_COOKIEFILE, dirname(__FILE__).'/cookies.txt');

the result is the same: another cookie is generated. Since the values are different, I don't get the result.

Tried to operate in the same session (so without closing the curl connection after the first request), same result. I am aware that the cookie file is not created until the connection is closed, and also, that the order of the curl_setopt is important, tried to fiddle with them as well. Still, from a browser it works just fine.

Could someone tell me what's happening? Why the cookie is not set (or ignored?!...)? Even tho it's a https request, I am not aware that it would need some kind of certificate or such (in which case probably the request wouldn't return the page, complaining about it...).

Thanks in advance.


Edit: Forgot to mention, I've also tried to use

curl_setopt($curl, CURLOPT_COOKIE, $cookiesStringToPass);

without success.


Edit 2: Also tried to rebuild the whole header, as it comes from the server, and also tried to include the referrer, to no avail.


Edit 3: From the command line, dumping the header from the search page:

curl -v --dump-header headers https://www.eofcom.admin.ch/eofcom/public/searchEofcom_InaFree.do > aa.html

and then reusing it to POST to the result page

curl -v -L -b headers --data "nrt=2&pnp=000991&doSearchFreeByNumber=Search" https://www.eofcom.admin.ch/eofcom/public/listEofcom_InaFree.do > ab.html

still produces the same, wrong result, tho this time the cookie is set correctly.

The problem wasn't related to the cookie. There was a second session id, set by javascript on the action of the form, in form of a get parameter. Once passing that too, all worked as expected.