[英]Use Browser cookies in PHP Curl to log-in to a website
I'm trying to get some data from a website, this data is only available for signed users, but I'm not able to sign-in using CURL because it uses CAPTCHA, so I'm trying to use the cookies that are generated by me signing up on a browser to let CURL access the pages that are only allowed to signed users. 我正在尝试从网站上获取一些数据,该数据仅适用于已签名的用户,但是我无法使用CURL进行登录,因为它使用了CAPTCHA,所以我正在尝试使用生成的Cookie我在浏览器上进行了注册,以允许CURL访问仅允许已签名用户使用的页面。
So let's say that I've copied all cookies that this website has generated when I logged in using my browser to a cookie.txt file. 假设我已将使用浏览器登录时该网站生成的所有cookie复制到cookie.txt文件中。 I have two questions: 我有两个问题:
In what format should I store the cookies in cookie.txt? 我应该以哪种格式将cookie存储在cookie.txt中?
How can I load these cookies for CURL to use? 如何加载这些Cookie以供CURL使用?
$curl = curl_init();
//load cookies from cookie.txt code
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,10);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
$contents = curl_exec($curl);
curl_close($curl);
echo $contents;
Edit 1 编辑1
$url="https://google.com";
$curl = curl_init();
curl_setopt($curl, CURLOPT_COOKIEJAR, '/Testing/cookie.txt');
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,10);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERAGENT, $userAgent); /
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
$contents = curl_exec($curl);
curl_close($curl);
echo $contents;
Tried the above code to get an example of the cookie file, but it seems like cookie.txt remain empty. 尝试了以上代码以获取Cookie文件的示例,但cookie.txt似乎保持空白。
Edit 2 编辑2
I changed the path for '/Testing/cookie.txt'
to __DIR__."/cookie.txt"
and the cookies where generated in the file, I will try to modify them and use COOKIEFILE and see if it will work. 我将'/Testing/cookie.txt'
的路径更改为__DIR__."/cookie.txt"
并在文件中生成了cookie,我将尝试对其进行修改并使用COOKIEFILE,看看它是否可以工作。
Edit 3 编辑3
Followed the answer provided below, and it worked as intended. 遵循以下提供的答案,并且按预期方式工作。
Try to add options for save cookies: 尝试添加用于保存Cookie的选项:
curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookies.txt');
To verify the format of a cookie file: You can set 验证Cookie文件的格式:您可以设置
curl_setopt($ch, CURLOPT_COOKIEJAR, "path_to_the_cookie_file")
next run a CURL request to any website that sets cookies, then check the format of the set cookie file. 接下来,向任何设置cookie的网站运行CURL请求,然后检查设置的cookie文件的格式。
To send your cookies: Then build your cookie file with the desired cookies and use the setting 发送Cookie:然后,使用所需的Cookie构建Cookie文件并使用设置
curl_setopt($ch, CURLOPT_COOKIEFILE, "path_to_the_cookie_file")
to make your CURL request send the cookies. 使您的CURL请求发送cookie。
Edit #1: From the official PHP documentation ( http://php.net/manual/en/function.curl-setopt.php ): 编辑#1:从正式的PHP文档( http://php.net/manual/en/function.curl-setopt.php ):
The cookie file can be in Netscape format, or just plain HTTP-style headers dumped into a file. cookie文件可以是Netscape格式,也可以是纯HTTP样式的标头转储到文件中。
Edit #2: this is an example of a cookie file generated by CURL ( source ): 编辑#2:这是CURL( source )生成的cookie文件的示例:
# Netscape HTTP Cookie File
# This file was generated by libcurl! Edit at your own risk.
.auto.com TRUE / FALSE 1452087781 ___suid 2ecfe4287cbeacd8399eaf98bec9ce0b.59089b9d033bc7c6dce8ea2fca139920
.auto.com TRUE / FALSE 1452865380 all7_user_region_confirmed 1
.auto.com TRUE / FALSE 1452865380 geo_location a%3A3%3A%7Bs%3A7%3A%22city_id%22%3Ba%3A0%3A%7B%7Ds%3A9%3A%22region_id%22%3Ba%3A1%3A%7Bi%3A0%3Bi%3A89%3B%7Ds%3A10%3A%22country_id%22%3Ba%3A0%3A%7B%7D%7D
.auto.com TRUE / FALSE 1423921380 autoru_sid ee094d60fa32eada_daf2da69dc79a59b7c8702a29554abbc
.auth.auto.com TRUE / FALSE 1421329026 autoru_sid
.auth.auto.com TRUE / FALSE 1421329026 autoru_sid_key
.auto.com TRUE / FALSE 1421329026 cc6882cb6b6f0c912cf9589734fcc1e6
.auto.com TRUE / FALSE 1452865027 user_name igor.savinkin5%40gmail.com
.auto.com TRUE / FALSE 1452865027 username igor.savinkin5%40gmail.com
Edit #3: What are all those entries in my cookies.txt file? 编辑#3:我的cookies.txt文件中所有这些条目是什么? ( http://www.cookiecentral.com/faq/#3.5 ) ( http://www.cookiecentral.com/faq/#3.5 )
From left-to-right, here is what each field represents: 从左到右,这是每个字段代表的内容:
domain - The domain that created AND that can read the variable. 域-创建AND且可以读取变量的域。
flag - A TRUE/FALSE value indicating if all machines within a given domain can access the variable. flag-一个TRUE / FALSE值,指示给定域内的所有计算机是否可以访问该变量。 This value is set automatically by the browser, depending on the value you set for domain. 该值由浏览器自动设置,具体取决于您为域设置的值。
path - The path within the domain that the variable is valid for. path-变量在其域内有效的路径。
secure - A TRUE/FALSE value indicating if a secure connection with the domain is needed to access the variable. 安全-一个TRUE / FALSE值,指示是否需要与域的安全连接才能访问该变量。
expiration - The UNIX time that the variable will expire on. expiration-变量到期的UNIX时间。 UNIX time is defined as the number of seconds since Jan 1, 1970 00:00:00 GMT. UNIX时间定义为自格林尼治标准时间1970年1月1日00:00:00起经过的秒数。
name - The name of the variable. name-变量的名称。
value - The value of the variable. value-变量的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.