httpclient爬虫怎么获取cookies

2025-04-02 14:10:31
推荐回答(2个)
回答1:

需要源码入下。

private function parseCookieLine($line)
{
$now = time();
$cookie = array('name' => '', 'value' => '', 'expires' => null, 'path' => '/');
$cookie['domain'] = $this->req->getHeader('host');
$tmpa = explode(';', substr($line, 12));
foreach ($tmpa as $tmp)
{
if (($pos = strpos($tmp, '=')) === false)
continue;
$k = trim(substr($tmp, 0, $pos));
$v = trim(substr($tmp, $pos + 1));
if ($cookie['name'] === '')
{
$cookie['name'] = $k;
$cookie['value'] = $v;
}
else
{
$k = strtolower($k);
if ($k === 'expires')
{
$cookie[$k] = strtotime($v);
if ($cookie[$k] < $now)
$cookie['value'] = '';
}
else if ($k === 'domain')
{
$pos = strpos($cookie['domain'], $v);
if ($pos === 0
|| substr($cookie['domain'], $pos, 1) === '.'
|| substr($cookie['domain'], $pos + 1, 1) === '.')
{
$cookie[$k] = $v;
}
}
else if (isset($cookie[$k]))
{
$cookie[$k] = $v;
}
}
}
if ($cookie['name'] !== '')
return $cookie;
return false;
}

回答2:

把header里面的cookie取下,下次递交前,设置cookie