Javascript 将Cookie传递到网站

Javascript 将Cookie传递到网站,javascript,php,cookies,curl,fopen,Javascript,Php,Cookies,Curl,Fopen,我正在尝试使用cURL获取此图像: http://habbo.com/habbo-imaging/avatarimage?user=-Claire%21&action=std&direction=3&head_direction=3&gesture=std&size=m&img_format=gif 如果您在浏览器中访问此网站,您将看到一幅图像,但如果我使用Curl: $ch = curl_init("http://habbo.com/habbo

我正在尝试使用cURL获取此图像:

http://habbo.com/habbo-imaging/avatarimage?user=-Claire%21&action=std&direction=3&head_direction=3&gesture=std&size=m&img_format=gif
如果您在浏览器中访问此网站,您将看到一幅图像,但如果我使用Curl:

$ch = curl_init("http://habbo.com/habbo-imaging/avatarimage?user=-Claire%21&action=std&direction=3&head_direction=3&gesture=std&size=m&img_format=gif");
$fp = fopen($img, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_exec($ch);
curl_close($ch);
fclose($fp);
图像文件仅包含以下HTML代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function getCookie(c_name) { // Local function for getting a cookie value
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start!=-1) {
        c_start=c_start + c_name.length + 1;
        c_end=document.cookie.indexOf(";", c_start);

        if (c_end==-1) 
            c_end = document.cookie.length;

        return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}
function setCookie(c_name, value, expiredays) { // Local function for setting a value of a cookie
    var exdate = new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires=" + exdate.toGMTString()) + ";path=/";
}
function getHostUri() {
    var loc = document.location;
    return loc.toString();
}
setCookie('YPF8827340282Jdskjhfiw_928937459182JAX666', '176.198.73.37', 10);
location.href = getHostUri();
</script>
</head>
<body>
<noscript>This site requires JavaScript and Cookies to be enabled. Please change your browser settings or upgrade your browser.</noscript>
</body>
</html>

我认为这会起作用,因为在我看来,我确实需要一块饼干来获得图像,但它不起作用。是否有人了解该过程并能告诉我如何下载该图像?

首先,您必须执行两个请求:第一个用于初始下载,第二个用于传递cookie


其次,这个cookie是由Javascript设置的。cURL对付不了这样的饼干。所以您必须手动处理这个cookie:从html解析它的值,并将其传递给下一个cURL请求。您可以通过设置选项CURLOPT_COOKIE来实现这一点。

无耻的插件:如果除了Javascript COOKIE之外还有额外的HTTP COOKIE,您可以将[COOKIE Jar Writer]()与CURLOPT_COOKIEFILE/CURLOPT_COOKIEFILE选项结合使用。这个PHP类允许在cURL请求序列中向CookieJAR文件添加自定义cookie变量。谢谢!我会看看的!:)
YPF8827340282Jdskjhfiw_928937459182JAX666=176.198.73.37;