简体   繁体   English

PHP / cURL如何获取请求Cookie

[英]PHP/cURL how to get Request Cookies

Hy, HY,

I have a bit of a problem here. 我这里有点问题。 One client of mine asked to create a login script for a website built in ASP. 我的一个客户要求为使用ASP构建的网站创建登录脚本。 As much as I can take and use Response Cookies, I am not able to take and use Request Cookies. 我可以使用和使用响应Cookie最多,但不能使用和使用请求Cookie。 I tried, but they are not saved in my cookie files. 我尝试过,但是它们没有保存在我的Cookie文件中。 As such, I cannot login into the very website. 因此,我无法登录该网站。 Its address is http://www.itraceuk.co.uk/Default.asp?secure=signin 它的地址是http://www.itraceuk.co.uk/Default.asp?secure=signin

Here is the code I am currently using: 这是我当前正在使用的代码:

$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);
<?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));

Set cookie folder/file permissions to writeable by PHP and Server. 将Cookie文件夹/文件权限设置为可由PHP和Server写入。

Reuse the cookie data after the login. 登录后重用cookie数据。

Curl Post to the login form with correct credentials, that logs you in and sets the cookie (curl CURLOPT_POSTFIELDS). Curl使用正确的凭据发布到登录表单,该凭据可以登录并设置cookie(curl CURLOPT_POSTFIELDS)。 Then reuse this cookie for the second curl request (this code here). 然后,将该Cookie重复用于第二个curl请求(此处为此代码)。

For a complete example of a HTTPS cURL login, please see: https://stackoverflow.com/a/10307956/1163786 有关HTTPS cURL登录的完整示例,请参阅: https : //stackoverflow.com/a/10307956/1163786

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

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