cURL - 不工作 - 警告:无法在cookie.txt中保存cookie

时间:2018-06-08 07:58:49

标签: php http curl

我对cURL完全陌生,但这就是我想要做的事情:

我的一个供应商有一个产品Feed,我可以在登录他们的网站后下载。这没关系,但我想自动化它,所以我试图将以下脚本放在一起(谷歌和堆栈信息) - 出于安全原因,我已经更改了用户名和密码但我100%确定他们是正确的:

<?php

//The username or email address of the account.
define('USERNAME', 'myusername');

//The password of the account.
define('PASSWORD', 'mypassword');

//Set a user agent. This basically tells the server that we are using Chrome ;)
define('USER_AGENT', 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.2309.372 Safari/537.36');

//Where our cookie information will be stored (needed for authentication).
define('COOKIE_FILE', '/cookie.txt');

//URL of the login form.
define('LOGIN_FORM_URL', 'https://www.treatgifts.com/customer/sign-in.aspx');

//Login action URL. Sometimes, this is the same URL as the login form.
define('LOGIN_ACTION_URL', 'https://www.treatgifts.com/customer/sign-in.aspx');


$f = fopen('log.txt', 'w'); // file to write request header for debug purpose


//Get __VIEWSTATE & __EVENTVALIDATION

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, USER_AGENT);

$html = curl_exec($ch);

curl_close($ch);

preg_match('~<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(.*?)" />~', $html, $viewstate);
preg_match('~<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="(.*?)" />~', $html, $eventValidation);

$viewstate = $viewstate[1];
$eventValidation = $eventValidation[1];

$f = fopen('log.txt', 'w'); // file to write request header for debug purpose


 //Start Login process

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, LOGIN_FORM_URL);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILE);
curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_FILE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_REFERER, LOGIN_FORM_URL);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_STDERR, $f);
curl_setopt($ch, CURLOPT_USERAGENT, USER_AGENT);

// Collecting all POST fields
$postfields = array(
    'ctl00$BodyContentHolder$ucSecureSignInPrompt$txtCustomerEmail' => USERNAME,
    'ctl00$BodyContentHolder$ucSecureSignInPrompt$txtCustomerPassword' => PASSWORD,

'tl00$BodyContentHolder$ucSecureSignInPrompt$btnLogin'=>'LOGIN',
    'ctl00$BodyContentHolder$ucSecureSignInPrompt$txtCustomerEmail' => USERNAME,
    'ctl00$BodyContentHolder$ucSecureSignInPrompt$txtCustomerPassword' => PASSWORD,
'ctl00$ToolkitScriptManager1'=>'ctl00$BodyContentHolder$CartStatusUpdatePanel|ctl00$BodyContentHolder$ucSecureSignInPrompt$btnLogin'


);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
$ret = curl_exec($ch); // Get result after login page.
print $ret; ?>

当我运行代码时,它只是将我引导到登录页面,而没有登录。我将日志写入文件,这是我得到的:

*   Trying 89.151.77.75...
* TCP_NODELAY set
* Connected to www.treatgifts.com (89.151.77.75) port 443 (#0)
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-SHA384
* ALPN, server did not agree to a protocol
* Server certificate:
*  subject: OU=Domain Control Validated; OU=PositiveSSL; CN=www.treatgifts.com
*  start date: Apr 20 00:00:00 2018 GMT
*  expire date: Apr 19 23:59:59 2020 GMT
*  subjectAltName: host "www.treatgifts.com" matched cert's "www.treatgifts.com"
*  issuer: C=GB; ST=Greater Manchester; L=Salford; O=COMODO CA Limited; CN=COMODO RSA Domain Validation Secure Server CA
*  SSL certificate verify ok.
> POST /customer/sign-in.aspx HTTP/1.1

Host: www.treatgifts.com

User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.2309.372 Safari/537.36

Accept: */*

Referer: https://www.treatgifts.com/customer/sign-in.aspx

Content-Length: 758

Expect: 100-continue

Content-Type: multipart/form-data; boundary=------------------------58b9f0207a2bb57b



< HTTP/1.1 100 Continue

< HTTP/1.1 200 OK

< Cache-Control: private

< Content-Type: text/html; charset=utf-8

< Server: Microsoft-IIS/8.5

* Added cookie ASP.NET_SessionId="ehavykaxsnimn15gngsavho1" for domain www.treatgifts.com, path /, expire 0
< Set-Cookie: ASP.NET_SessionId=ehavykaxsnimn15gngsavho1; path=/; HttpOnly

< X-AspNet-Version: 4.0.30319

* Added cookie VTN="f818033a-08f4-401c-a6c9-d59688fad66b-0028710" for domain www.treatgifts.com, path /, expire 1529020828
< Set-Cookie: VTN=f818033a-08f4-401c-a6c9-d59688fad66b-0028710; expires=Fri, 15-Jun-2018 00:00:28 GMT; path=/

* Added cookie USID="ehavykaxsnimn15gngsavho1" for domain www.treatgifts.com, path /, expire 1529020828
< Set-Cookie: USID=ehavykaxsnimn15gngsavho1; expires=Fri, 15-Jun-2018 00:00:28 GMT; path=/

< X-Powered-By: ASP.NET

< Date: Fri, 08 Jun 2018 00:00:28 GMT

< Content-Length: 23444

< 

* Connection #0 to host www.treatgifts.com left intact
* WARNING: failed to save cookies in cookie.txt

正如您所看到的那样,底部有一个警告,我怀疑这是一个问题?我试图解决的问题是:

  • 手动创建cookie.txt并授予它777权限。
  • 使用绝对路径

这些都没有奏效。

我也没有在控制台中看到POST命令

有人可以提供任何建议吗?

由于 克里斯

0 个答案:

没有答案