问题
I'm totally new to cURL but here's what I'm wanting to do:
One of my suppliers has a product feed that I can download once I login to their site. This is ok but I want to automate it so I've tried to put the following script together (google and Stack info) - I've changed the username and password for security reasons but I'm 100% sure they're correct:
<?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; ?>
When I run the code it just directs me to the login page, without logging me in. I write the log to a file and here is what I get:
* 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
As you can see there is a warning at the bottom, which I suspect is causing the issue? What I've tried to do to fix this is:
- manually create cookie.txt andgive it 777 permissions.
- use the absolute path
None of these have worked.
I also don't see a POST command in console
Can anyone offer any advise?
Thanks Chris
来源:https://stackoverflow.com/questions/50756026/curl-not-working-warning-failed-to-save-cookies-in-cookie-txt