简体   繁体   English

PHP cURL将Cookie传递到网站

[英]PHP cURL pass Cookie to website

I am trying to fetch this image using cURL: 我正在尝试使用cURL获取此图像:

http://habbo.com/habbo-imaging/avatarimage?user=-Claire%21&action=std&direction=3&head_direction=3&gesture=std&size=m&img_format=gif

If you visit this Site in the Browser, you will see an image, but if I use Curl: 如果您在浏览器中访问此站点,则会看到一个图像,但是如果我使用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);

The Image File just contains this HTML Code: 图像文件仅包含以下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>

As you can see I tried to include a cookies.txt which is in the same directory as the script that is executed. 如您所见,我尝试将cookies.txt包含在与执行的脚本相同的目录中。 The cookies.txt has 777 rights and looks like this: cookies.txt具有777权限,如下所示:

YPF8827340282Jdskjhfiw_928937459182JAX666=176.198.73.37;

I thought that would do the trick, as it seems to me that I do need a cookie to get the image, but it doesn't work. 我认为这可以解决问题,因为在我看来我确实需要一个cookie来获取图像,但是它不起作用。 Does anyone understand the procedure and can tell me how to download that image? 有谁了解该程序,并且可以告诉我如何下载该图像?

Firstly you have to perform two requests: 1st for initial download and 2nd for passing cookie. 首先,您必须执行两个请求:第一个请求进行初始下载,第二个请求传递cookie。

Secondly this cookie is set by Javascript. 其次,此cookie由Javascript设置。 cURL can't handle such cookies. cURL无法处理此类cookie。 So you have to handle this cookie manually: parse its value from html and pass it to next cURL request. 因此,您必须手动处理此cookie:从html解析其值,并将其传递给下一个cURL请求。 You can do it by setting option CURLOPT_COOKIE. 您可以通过设置选项CURLOPT_COOKIE来实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM