PHP / cURL如何获取Request Cookies

时间:2014-10-14 22:26:26

标签: php asp.net curl cookies

HY,

我在这里遇到了一些问题。我的一个客户要求为ASP构建的网站创建登录脚本。尽可能多地使用Response Cookies,我无法使用Request Cookies。我试过了,但它们没有保存在我的cookie文件中。因此,我无法登录该网站。其地址为http://www.itraceuk.co.uk/Default.asp?secure=signin

以下是我目前使用的代码:

$ch4 = curl_init(); 
curl_setopt ($ch4, CURLOPT_URL, $posturl); 
curl_setopt($ch4, CURLOPT_HEADER, true);
curl_setopt ($ch4, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt ($ch4, CURLOPT_SSL_VERIFYHOST, false); 
curl_setopt ($ch4, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36"); 
curl_setopt ($ch4, CURLOPT_TIMEOUT, 60); 
curl_setopt ($ch4, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt ($ch4, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch4, CURLOPT_COOKIESESSION, 1);
curl_setopt ($ch4, CURLOPT_COOKIEJAR, $cookie2); 
curl_setopt ($ch4, CURLOPT_COOKIEFILE, $cookie2); 
curl_setopt ($ch4, CURLOPT_REFERER, 'http://www.itraceuk.co.uk/Default.asp?secure=signin'); 
$result4 = curl_exec ($ch4); 
curl_close($ch4);

1 个答案:

答案 0 :(得分:0)

<?php

$posturl = 'http://www.itraceuk.co.uk/Default.asp?secure=signin';

$cookie = __DIR__ . '/cookie.txt'; // file and folder must be writeable

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $posturl);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 ( .NET CLR 3.5.30729)");
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);

curl_setopt($ch, CURLOPT_REFERER, 'http://www.itraceuk.co.uk/Default.asp?secure=signin');

$result = curl_exec($ch);
curl_close($ch);

var_dump($result);

var_dump(file_get_contents($cookie));

将cookie文件夹/文件权限设置为可由PHP和Server写入。

登录后重复使用cookie数据。

使用正确的凭据发布到登录表单,登录并设置cookie(curl CURLOPT_POSTFIELDS)。然后将此cookie重新用于第二个curl请求(此处为此代码)。

有关HTTPS cURL登录的完整示例,请参阅:https://stackoverflow.com/a/10307956/1163786