php cURL cookie is not passed -
there site, search phone numbers. need make either php script
or curl command
able search cron job
.
when visit search page, "session" cookie created, used obtain results. on result page, in case cookie missing or contains wrong information, search not yield results.
so figured visit search page, grab cookie, post
cookie, alongside search parameters need result page, different (the search page's form action points that).
the first part done. able grab cookie, either parsing 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] $item) { parse_str($item, $cookie); $cookies = array_merge($cookies, $cookie); } $cookiesstringtopass = ''; foreach ($cookies $name=>$value) { if ($cookiesstringtopass) { $cookiesstringtopass .= ';'; } $cookiesstringtopass .= $name . '=' . addslashes($value); } // $cookiesstringtopass contains cookie names , values
or storing in file using:
curl_setopt($curl, curlopt_cookiejar, dirname(__file__).'/cookies.txt'); curl_setopt($curl, curlopt_cookiefile, dirname(__file__).'/cookies.txt');
so far good. in second part, need submit/post information obtain result (again, it's different page search one), should pass cookie name/value, not happening. either set explicitly cookie
in header
, 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 set file generated before:
curl_setopt($curl, curlopt_cookiefile, dirname(__file__).'/cookies.txt');
the result same: cookie generated. since values different, don't result.
tried operate in same session (so without closing curl connection after first request), same result. aware cookie file not created until connection closed, , also, order of curl_setopt
important, tried fiddle them well. still, browser works fine.
could tell me what's happening? why cookie not set (or ignored?!...)? tho it's https
request, not aware need kind of certificate or such (in case request wouldn't return page, complaining it...).
thanks in advance.
edit: forgot mention, i've tried use
curl_setopt($curl, curlopt_cookie, $cookiesstringtopass);
without success.
edit 2: tried rebuild whole header
, comes server, , tried include referrer
, no avail.
edit 3: command line, dumping header search page:
curl -v --dump-header headers https://www.eofcom.admin.ch/eofcom/public/searcheofcom_inafree.do > aa.html
and reusing post
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 same, wrong result, tho time cookie set correctly.
the problem wasn't related cookie. there second session id, set javascript on action of form, in form of parameter. once passing too, worked expected.
Comments
Post a Comment