【发布时间】:2017-10-29 10:54:19
【问题描述】:
我需要编写一个机器人来登录网站并进行一些查询,问题是这个网站是用 ASP.NET MVC 编写的,需要两个 cookie 来识别用户!我可以使用我的 PHP curl 保存一个 cookie,但无法存储另一个 .AspNet.ApplicationCookie(大约 400KB)!
这是我的代码:
$ch = curl_init('MY URL...');
$cookie = __DIR__ . '/cookie.txt';
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "UserName=123&Password=123");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
$result = curl_exec($ch);
var_dump($result);
【问题讨论】:
-
cookie.txt 文件会发生什么,它保持为空?我认为您需要添加“CURLOPT_COOKIESESSION”参数。示例: curl_setopt($ch, CURLOPT_COOKIESESSION, true);如果这不起作用,请检查您的 cookie.txt 文件权限。可能是因为权限错误,PHP 无法写入文件。
-
不,我的 cookie.txt 不是空的,它包含一个 cookie 的所有信息!我尝试了 CURLOPT_COOKIESESSION 选项,但仍然没有乐趣!
标签: php asp.net curl cookies web-crawler