2

我有一个 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;
}
4

1 回答 1

-1

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

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

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

setcookie("TestCookie", $value, time()+3600); 
于 2013-01-25T05:00:48.393 回答