如何使用卷曲设置浏览器 Cookie


How To Set Browser Cookies With Curl

我有一个如下所示的curl函数。要加载此功能,我使用此<img src="http://site.com/pxl.php?i=1.jpg" height="1" width="1" />但是当我这样做时,cookie不会添加到我的浏览器中。有没有办法让当 curl 在 url 上运行时,我从 url 收集 cookie 并将其设置为使用访问<img src="http://site.com/pxl.php?i=1.jpg" height="1" width="1" />的浏览器

function get_content($url,$ref)
{
$browser = $_SERVER['HTTP_USER_AGENT'];
$ch = curl_init();
$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";
$header[] = "Pragma: "; // browsers keep this blank.
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $browser);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $ref);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, false);
$html = curl_exec($ch);
curl_close ($ch);
return $html;
}

要使用 cURL 将 Cookie 发送到页面,请使用CURLOPT_COOKIE选项:

curl_setopt($ch, CURLOPT_COOKIE, 'user=alice; activity=knitting');

要为脚本设置 cookie,请使用 setcookie():

setcookie("TestCookie", $value, time()+3600);