CURL and PHP - keep cookies values for next request -
i have following problem - after send post request page, let's example.com/search.php i'm getting first page of results , links pages looks this:
example.com/search.php?start=15
but query search stored in cookies this:
phpbb2mysql_sid 5e9f95bceb61e9634ce3df03123d9446
which save in cookie jar, when trying access next page results looks curl doesn't use cookies first query, replace them new ones, page i'm trying open search empty. here's code:
public function getpage($url) { $cookie_file_path = 'public/userfiles/cookie.txt'; $ch = curl_init(); curl_setopt($ch, curlopt_header, false); curl_setopt($ch, curlopt_nobody, false); curl_setopt($ch, curlopt_url, $this->parent_url); curl_setopt($ch, curlopt_ssl_verifyhost, 0); curl_setopt($ch, curlopt_cookiejar, $cookie_file_path); curl_setopt($ch, curlopt_cookie, $cookie_file_path); curl_setopt($ch, curlopt_useragent, "mozilla/5.0 (windows; u; windows nt 5.0; en-us; rv:1.7.12) gecko/20050915 firefox/1.0.7"); curl_setopt($ch, curlopt_returntransfer, 1); curl_setopt($ch, curlopt_referer, $this->parent_url); curl_setopt($ch, curlopt_ssl_verifypeer, 0); curl_setopt($ch, curlopt_followlocation, 0); if (!empty($this->post)) { curl_setopt($ch, curlopt_customrequest, "post"); curl_setopt($ch, curlopt_post, 1); curl_setopt($ch, curlopt_postfields, $this->post); } curl_exec($ch); //page content want grab curl_setopt($ch, curlopt_customrequest, "get"); curl_setopt($ch, curlopt_post, 0); curl_setopt($ch, curlopt_url, $url); // following request should use cookies previous, seems doesn't // cause search results empty $html = curl_exec($ch); curl_close($ch); return $html; }
any appreciated.
i have own cookie routine.
i http response header
remove response header $html
then grab cookies response header, putting them in array
then save array next time needed
change curlopt_header
true
curl_setopt($ch, curlopt_header, true); $html= curl_exec($ch); $skip = intval(curl_getinfo($ch, curlinfo_header_size)); $requestheader= substr($html,0,$skip); $html = substr($html,$skip); $e = 0; while(true){ $s = strpos($requestheader,'set-cookie: ',$e); if (!$s){break;} $s += 12; $e = strpos($requestheader,';',$s); $cookie = substr($requestheader,$s,$e-$s) ; $s = strpos($cookie,'='); $key = substr($cookie,0,$s); $value = substr($cookie,$s); $cookies[$key] = $value; } $fp = fopen('/home/user/public_html/cookies.ser' ,'w'); fwrite($fp,serialize($cookies)); fclose($fp);
the retrieve:
$cookies= unserialize(file_get_contents('/home/user/public_html/cookies.ser')); $cookie = ''; $show = ''; $head = ''; $delim = ''; foreach ($cookies $k => $v){ $cookie .= "$delim$k$v"; $delim = '; '; } curl_setopt($ch, curlopt_cookie, $cookie );
Comments
Post a Comment