无法使用cURL连接到HTTPS站点怎么办
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法使用cURL连接到HTTPS站点怎么办相关的知识,希望对你有一定的参考价值。
参考技术A 解决方法为在curl请求时,加入:复制代码代码如下:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); // 从证书中检查SSL加密算法是否存在
curl https请求代码
复制代码代码如下:
<?php
/** curl 获取 https 请求
* @param String $url 请求的url
* @param Array $data 要发送的数据
* @param Array $header 请求时发送的header
* @param int $timeout 超时时间,默认30s
*/
function curl_https($url, $data=array(), $header=array(), $timeout=30)
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); // 从证书中检查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
$response = curl_exec($ch);
if($error=curl_error($ch))
die($error);
curl_close($ch);
return $response;
// 调用
$url = '';
$data = array('name'=>'fdipzone');
$header = array();
$response = curl_https($url, $data, $header, 5);
echo $response;
?>
尝试使用 CURL 连接到 ASPX 站点?
【中文标题】尝试使用 CURL 连接到 ASPX 站点?【英文标题】:Trying to connect to ASPX site using CURL? 【发布时间】:2013-02-26 13:08:33 【问题描述】:我正在尝试登录这个网址:
http://www.kalahari.com/marketplace/default.aspx
正在提交的两个字段被标记:
ctl00$ctl00$ucMarketPlaceSupportNavigation$txtMPTopSignInEmail
ctl00$ctl00$ucMarketPlaceSupportNavigation$txtMPTopSignInPasswordTextNormal
到目前为止我尝试使用的代码:
$username = 'XXXXXXX';
$password = 'XXXXXXX';
$loginUrl = 'http://www.kalahari.com/marketplace/default.aspx';
$cookie = 'cookies.txt';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $loginUrl );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE );
curl_setopt($ch, CURLOPT_COOKIEJAR , $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_HEADER, FALSE );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$ret = curl_exec($ch); //access login page
// Collecting all POST fields
$postfields = array();
$postfields['ctl00$ctl00$ucMarketPlaceSupportNavigation$txtMPTopSignInEmail'] = $username;
$postfields['ctl00$ctl00$ucMarketPlaceSupportNavigation$txtMPTopSignInPasswordTextNormal'] = $password;
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
$ret = curl_exec($ch);//Get result after login page.
print $ret;
但这只会让我回到原来的登录页面...甚至没有错误消息。
然后我查看了发布的内容,我看到了:
Request URL:http://www.kalahari.com/marketplace/default.aspx
Request Method:POST
Status Code:302 Found
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:2596
Content-Type:application/x-www-form-urlencoded
Cookie:VISITORID=9840A7E31683480CB19A66FB8AA73BFC; ASP.NET_SessionId=foous3ftij3os2vvr1wbm3mm; __utma=160092839.590473234.1362995010.1362995010.1362995010.1; __utmc=160092839; __utmz=160092839.1362995010.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); _SUPERFLY_nosample=1; shopperName=; signin=0; kalahariShopperId=922859656760417F99E83D5B1427115F; surfLang=ENG; prefLanguage=en-ZA; _chartbeat2=1yx62ww1m7xz1o84.1360134968807.1363000295875.00000000000001
Host:www.kalahari.com
Origin:http://www.kalahari.com
Referer:http://www.kalahari.com/marketplace/default.aspx
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.152 Safari/537.22
Form Dataview sourceview URL encoded
__EVENTTARGET:
__EVENTARGUMENT:
__VIEWSTATE:/wEPDwULLTEzODYxODcwMTMPZBYCZg9kFgJmD2QWAgIBD2QWCAICD2QWBAIDDw8WAh4LTmF2aWdhdGVVcmwFNH4vcGlwZWxpbmUvc2lnbmluLmFzcHg/UmV0dXJuVVJMPS9zZWN1cmUvbWFya2V0cGxhY2VkZAIHDxYCHgdWaXNpYmxlaBYCAgEPDxYCHwFoZGQCBA9kFgICAQ9kFgICAQ8WAh8BaBYCAgEPZBYQAgEPDxYGHghDc3NDbGFzcwULYWN0aXZlX2xpbmseB0VuYWJsZWRnHgRfIVNCAgJkZAIDDw8WBh8CBQVsaW5rcx8DZx8EAgJkZAIFDw8WBh8CBQ1kaXNhYmxlX2xpbmtzHwNoHwQCAmRkAgcPDxYGHwIFDWRpc2FibGVfbGlua3MfA2gfBAICZGQCCQ8PFgYfAgUNZGlzYWJsZV9saW5rcx8DaB8EAgJkZAILDw8WBh8CBQ1kaXNhYmxlX2xpbmtzHwNoHwQCAmRkAg0PDxYGHwIFDWRpc2FibGVfbGlua3MfA2gfBAICZGQCDw8PFgYfAgUNZGlzYWJsZV9saW5rcx8DaB8EAgJkZAIFD2QWAgIBD2QWBgIJDxAPZBYCHgdvbkNsaWNrBYQBamF2YXNjcmlwdDpWYWxpZGF0ZUxvZ2luUmFkaW9CdG5ZZXMoY3RsMDBfY3RsMDBfY3BsaE1haW5fY3BsaENvbnRlbnRfcmRsUGFzc3dvcmRZZXMsY3RsMDBfY3RsMDBfY3BsaE1haW5fY3BsaENvbnRlbnRfcmRsUGFzc3dvcmRObyk7ZGRkAgsPEA9kFgIfBQWDAWphdmFzY3JpcHQ6VmFsaWRhdGVMb2dpblJhZGlvQnRuTm8oY3RsMDBfY3RsMDBfY3BsaE1haW5fY3BsaENvbnRlbnRfcmRsUGFzc3dvcmRZZXMsY3RsMDBfY3RsMDBfY3BsaE1haW5fY3BsaENvbnRlbnRfcmRsUGFzc3dvcmRObyk7ZGRkAhUPD2QWAh8FBdMBamF2YXNjcmlwdDpWYWxpZGF0ZUxvZ2luKCdjdGwwMF9jdGwwMF9jcGxoTWFpbl9jcGxoQ29udGVudF9sYmxSZXN1bHQnLGN0bDAwX2N0bDAwX2NwbGhNYWluX2NwbGhDb250ZW50X3R4dFBhc3N3b3JkLGN0bDAwX2N0bDAwX2NwbGhNYWluX2NwbGhDb250ZW50X3JkbFBhc3N3b3JkWWVzLGN0bDAwX2N0bDAwX2NwbGhNYWluX2NwbGhDb250ZW50X3JkbFBhc3N3b3JkTm8pO2QCBg9kFgICBQ8PFgIfAAUafi9waXBlbGluZS9vcmRlcl9saXN0LmFzcHhkZBgBBR5fX0NvbnRyb2xzUmVxdWlyZVBvc3RCYWNrS2V5X18WAwUvY3RsMDAkY3RsMDAkY3BsaE1haW4kY3BsaENvbnRlbnQkcmRsUGFzc3dvcmRZZXMFLmN0bDAwJGN0bDAwJGNwbGhNYWluJGNwbGhDb250ZW50JHJkbFBhc3N3b3JkTm8FLmN0bDAwJGN0bDAwJGNwbGhNYWluJGNwbGhDb250ZW50JHJkbFBhc3N3b3JkTm99/wuPOuNOonYg5XWvf3RGR1YVkw==
__EVENTVALIDATION:/wEWDQLsuI7QDgKnpLoxApD7nfEPAvLAqqUGAp35/akJAqGiqqYPAsXC5NUHAsHJ5OMCAovxoc8LArq0mqAKApm+rVoC9dLe0Q8C5IvEsAlYqPIdcrZvBZcvYav7ATMf4Nhbfg==
ctl00$ctl00$ucMarketPlaceSupportNavigation$txtMPTopSignInEmail:XXXXXXXXX
ctl00$ctl00$ucMarketPlaceSupportNavigation$txtMPTopSignInPasswordTextNormal:Password
ctl00$ctl00$ucMarketPlaceSupportNavigation$txtMPTopSignInPassword:XXXXXXXXXXX
ctl00$ctl00$ucMarketPlaceSupportNavigation$btnSigninTop:Sign in
ctl00$ctl00$cplhMain$cplhContent$txtEmail:Email address
ctl00$ctl00$cplhMain$cplhContent$rdlPasswordYes:rdlPasswordYes
ctl00$ctl00$cplhMain$cplhContent$txtPasswordTextNormal:Password
ctl00$ctl00$cplhMain$cplhContent$txtPassword:
ctl00$ctl00$cplhMain$cplhContent$hdnEmailDefault:Email address
ctl00$ctl00$cplhMain$cplhContent$hdnPasswordDefault:Password
Response Headersview source
Cache-Control:private, no-cache="Set-Cookie"
Content-Length:146
Content-Type:text/html; charset=utf-8
Date:Mon, 11 Mar 2013 11:11:57 GMT
Etag:
Location:/marketplace/default.aspx
Server:Microsoft-IIS/6.0
Set-Cookie:.KALAHARINETAUTH=782A6F442823F8148FB113BA0BAF3A9A8DE253762A4ACFAA5E911E4721166F0EEC6A1891755133AADD28654CF0DAE3880CC2B84260F0B915C07897909CFB071495AF8EF05D1BD678DEE1933FCB08E5ECB1CF76462900681C7D4AE963C151E3079D95FBAD6466F0528787455A951D5EC0DA26F0E6CAA341E4C717D7F3BC01D182F488F47F; domain=.kalahari.com; path=/; HttpOnly
Set-Cookie:surfLang=ENG; domain=.kalahari.com; expires=Sat, 11-Mar-2023 11:11:57 GMT; path=/
Set-Cookie:prefLanguage=en-ZA; domain=www.kalahari.com; path=/
Set-Cookie:signin=1; domain=kalahari.com; path=/
Set-Cookie:tempshopperid=922859656760417F99E83D5B1427115F; domain=kalahari.com; path=/
Set-Cookie:kalahariShopperId=54B14971F72D426BA02DEF3A3D99DC93; domain=kalahari.com; expires=Sun, 17-Jan-2038 22:00:00 GMT; path=/
Set-Cookie:shopperName=XXXX; domain=kalahari.com; path=/
Set-Cookie:kalahariShopperEmail=XXX@XXXX.XXX; domain=kalahari.com; path=/
X-AspNet-Version:2.0.50727
X-Powered-By:ASP.NET
看起来我应该提交的不仅仅是用户名和密码。我究竟必须发布什么以及如何发布诸如“视图状态”之类的不是固定值的内容?
【问题讨论】:
【参考方案1】:这很可能是一个 CRSF 令牌。您将调用该页面,然后使用登录请求中返回的值。 DropBox 使用类似的过程。如果您从 https://github.com/jakajancar/DropboxUploader/ 获取 Dropbox 上传器脚本,您可以看到它是如何做到的。
【讨论】:
【参考方案2】:简介
你的脚本不工作的可能原因如下
缺少__VIEWSTATE
缺少__EVENTVALIDATION
无效的用户代理
使用ctl00$ctl00$ucMarketPlaceSupportNavigation$txtMPTopSignInPasswordTextNormal
代替ctl00$ctl00$ucMarketPlaceSupportNavigation$txtMPTopSignInPassword
作为密码
不完整的帖子字段
重复卷曲请求
登录过程中缺少 Coolie
典型的服务器请求
完整的长请求如下所示
POST http://www.kalahari.com/marketplace/default.aspx HTTP/1.1
Host: www.kalahari.com
Connection: keep-alive
Content-Length: 2584
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Origin: http://www.kalahari.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.160 Safari/537.22
Content-Type: application/x-www-form-urlencoded
Referer: http://www.kalahari.com/marketplace/default.aspx
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: ASP.NET_SessionId=t2dopenbp5zdfojmg1sd4m55; VISITORID=ECE2335BD47344E29A3D24E04FA31777; mpshopperid=A2E19BEB4D77484A89B9CD64BBB9C294; sign_up_track=sign_up; shopperName=; signin=0; kalahariShopperId=9D0B941AADCC48CA8ACB071C840A9CEB; __utma=46148088.1484253925.1363278451.1363278451.1363278451.1; __utmb=46148088.8.10.1363278451; __utmc=46148088; __utmz=46148088.1363278451.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); surfLang=ENG; prefLanguage=en-ZA; _chartbeat2=fn9a2z0breekvh98.1363278407516.1363278713879.00000000000001
__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwULLTEzODYxODcwMTMPZBYCZg9kFgJmD2QWAgIBD2QWCAICD2QWBAIDDw8WAh4LTmF2aWdhdGVVcmwFNH4vcGlwZWxpbmUvc2lnbmluLmFzcHg%2FUmV0dXJuVVJMPS9zZWN1cmUvbWFya2V0cGxhY2VkZAIHDxYCHgdWaXNpYmxlaBYCAgEPDxYCHwFoZGQCBA9kFgICAQ9kFgICAQ8WAh8BaBYCAgEPZBYQAgEPDxYGHghDc3NDbGFzcwULYWN0aXZlX2xpbmseB0VuYWJsZWRnHgRfIVNCAgJkZAIDDw8WBh8CBQVsaW5rcx8DZx8EAgJkZAIFDw8WBh8CBQ1kaXNhYmxlX2xpbmtzHwNoHwQCAmRkAgcPDxYGHwIFDWRpc2FibGVfbGlua3MfA2gfBAICZGQCCQ8PFgYfAgUNZGlzYWJsZV9saW5rcx8DaB8EAgJkZAILDw8WBh8CBQ1kaXNhYmxlX2xpbmtzHwNoHwQCAmRkAg0PDxYGHwIFDWRpc2FibGVfbGlua3MfA2gfBAICZGQCDw8PFgYfAgUNZGlzYWJsZV9saW5rcx8DaB8EAgJkZAIFD2QWAgIBD2QWBgIJDxAPZBYCHgdvbkNsaWNrBYQBamF2YXNjcmlwdDpWYWxpZGF0ZUxvZ2luUmFkaW9CdG5ZZXMoY3RsMDBfY3RsMDBfY3BsaE1haW5fY3BsaENvbnRlbnRfcmRsUGFzc3dvcmRZZXMsY3RsMDBfY3RsMDBfY3BsaE1haW5fY3BsaENvbnRlbnRfcmRsUGFzc3dvcmRObyk7ZGRkAgsPEA9kFgIfBQWDAWphdmFzY3JpcHQ6VmFsaWRhdGVMb2dpblJhZGlvQnRuTm8oY3RsMDBfY3RsMDBfY3BsaE1haW5fY3BsaENvbnRlbnRfcmRsUGFzc3dvcmRZZXMsY3RsMDBfY3RsMDBfY3BsaE1haW5fY3BsaENvbnRlbnRfcmRsUGFzc3dvcmRObyk7ZGRkAhUPD2QWAh8FBdMBamF2YXNjcmlwdDpWYWxpZGF0ZUxvZ2luKCdjdGwwMF9jdGwwMF9jcGxoTWFpbl9jcGxoQ29udGVudF9sYmxSZXN1bHQnLGN0bDAwX2N0bDAwX2NwbGhNYWluX2NwbGhDb250ZW50X3R4dFBhc3N3b3JkLGN0bDAwX2N0bDAwX2NwbGhNYWluX2NwbGhDb250ZW50X3JkbFBhc3N3b3JkWWVzLGN0bDAwX2N0bDAwX2NwbGhNYWluX2NwbGhDb250ZW50X3JkbFBhc3N3b3JkTm8pO2QCBg9kFgICBQ8PFgIfAAUafi9waXBlbGluZS9vcmRlcl9saXN0LmFzcHhkZBgBBR5fX0NvbnRyb2xzUmVxdWlyZVBvc3RCYWNrS2V5X18WAwUvY3RsMDAkY3RsMDAkY3BsaE1haW4kY3BsaENvbnRlbnQkcmRsUGFzc3dvcmRZZXMFLmN0bDAwJGN0bDAwJGNwbGhNYWluJGNwbGhDb250ZW50JHJkbFBhc3N3b3JkTm8FLmN0bDAwJGN0bDAwJGNwbGhNYWluJGNwbGhDb250ZW50JHJkbFBhc3N3b3JkTm99%2FwuPOuNOonYg5XWvf3RGR1YVkw%3D%3D&__EVENTVALIDATION=%2FwEWDQLsuI7QDgKnpLoxApD7nfEPAvLAqqUGAp35%2FakJAqGiqqYPAsXC5NUHAsHJ5OMCAovxoc8LArq0mqAKApm%2BrVoC9dLe0Q8C5IvEsAlYqPIdcrZvBZcvYav7ATMf4Nhbfg%3D%3D&ctl00%24ctl00%24ucMarketPlaceSupportNavigation%24txtMPTopSignInEmail=XXXXXXXXXX%40rmqkr.net&ctl00%24ctl00%24ucMarketPlaceSupportNavigation%24txtMPTopSignInPasswordTextNormal=Password&ctl00%24ctl00%24ucMarketPlaceSupportNavigation%24txtMPTopSignInPassword=XXXXXXXXXX&ctl00%24ctl00%24ucMarketPlaceSupportNavigation%24btnSigninTop=Sign+in&ctl00%24ctl00%24cplhMain%24cplhContent%24txtEmail=Email+address&ctl00%24ctl00%24cplhMain%24cplhContent%24rdlPasswordYes=rdlPasswordYes&ctl00%24ctl00%24cplhMain%24cplhContent%24txtPasswordTextNormal=Password&ctl00%24ctl00%24cplhMain%24cplhContent%24txtPassword=&ctl00%24ctl00%24cplhMain%24cplhContent%24hdnEmailDefault=Email+address&ctl00%24ctl00%24cplhMain%24cplhContent%24hdnPasswordDefault=Password
使用卷曲
$url = "http://www.kalahari.com/marketplace/default.aspx";
$ckfile = tempnam("/tmp", "CURLCOOKIE");
$useragent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.2 (KHTML, like Gecko) Chrome/5.0.342.3 Safari/533.2';
$username = "XXXXXXXXXX";
$password = "XXXXXXXXXX";
$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, $ckfile);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
$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];
/**
Start Login process
*/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_STDERR, $f);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
// Collecting all POST fields
$postfields = array();
$postfields['__EVENTTARGET'] = "";
$postfields['__EVENTARGUMENT'] = "";
$postfields['__VIEWSTATE'] = $viewstate;
$postfields['__EVENTVALIDATION'] = $eventValidation;
$postfields['ctl00$ctl00$ucMarketPlaceSupportNavigation$txtMPTopSignInEmail'] = $username;
$postfields['ctl00$ctl00$ucMarketPlaceSupportNavigation$txtMPTopSignInPasswordTextNormal'] = "Password";
$postfields['ctl00$ctl00$ucMarketPlaceSupportNavigation$txtMPTopSignInPassword'] = $password;
$postfields['ctl00$ctl00$ucMarketPlaceSupportNavigation$btnSigninTop'] = 'Sign in';
$postfields['ctl00$ctl00$cplhMain$cplhContent$txtEmail'] = 'Email address';
$postfields['ctl00$ctl00$cplhMain$cplhContent$rdlPasswordYes'] = 'Password';
$postfields['ctl00$ctl00$cplhMain$cplhContent$txtPassword'] = '';
$postfields['ctl00$ctl00$cplhMain$cplhContent$hdnEmailDefault'] = 'Email address';
$postfields['ctl00$ctl00$cplhMain$cplhContent$hdnPasswordDefault'] = 'Password';
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
$ret = curl_exec($ch); // Get result after login page.
print $ret;
经过测试并且工作得很好......
【讨论】:
为了补充这一点,__EVENTVALIDATION
是故意防止你尝试的那种抓取。每当服务器上运行任何东西时都会生成一个新值,而不仅仅是在整个页面呈现时,这可能包括更改单个下拉框的值。
@Baba,我尝试了您的代码并收到内部错误。我应该自己添加隐藏字段的值还是在加载时自动填充?
表示__EVENTVALIDATION
值无效; @IMSoP 是对的。但是如何解决呢?
这太棒了!! :D【参考方案3】:
我想试一试并让您的代码正常工作,请参见下文。关于我在做什么,我在代码中有一些 var_dump 和 cmets。
<?php
$username = 'test@example.com';
$password = 'example';
$loginUrl = 'http://www.kalahari.com/marketplace/default.aspx';
//on windows server you need full path or its not even used.
$cookie = dirname(__FILE__) . '\cookies1.txt';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $loginUrl );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE );
curl_setopt($ch, CURLOPT_COOKIEJAR , $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_HEADER, FALSE );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$ret = curl_exec($ch); //access login page
//check the contents of the cookie file. the sessionID, visitorID and kalaharishooperID should be the same with each refresh.
var_dump(implode('', file($cookie)));
//now get the viewstate from the value
//could be better with regex or anything, but this works.
$content = explode('value="', stristr($ret, 'id="__VIEWSTATE"'));
$viewstate = substr($content[1], 0, strpos($content[1], '"'));
var_dump($viewstate);
//now get the EVENTVALIDATION from the value
//could be better with regex or anything, but this works.
$content = explode('value="', stristr($ret, 'id="__EVENTVALIDATION"'));
$validation = substr($content[1], 0, strpos($content[1], '"'));
var_dump($validation);
//define post fields
//i removed all fields that are not required for the post
$postfields = array(
"__VIEWSTATE" => rawurlencode($viewstate),
"__EVENTVALIDATION" => rawurlencode($validation),
"ctl00%24ctl00%24ucMarketPlaceSupportNavigation%24txtMPTopSignInEmail" => rawurlencode($username),
"ctl00%24ctl00%24ucMarketPlaceSupportNavigation%24txtMPTopSignInPasswordTextNormal" => "Password",
"ctl00%24ctl00%24ucMarketPlaceSupportNavigation%24txtMPTopSignInPassword" => rawurlencode($password),
"ctl00%24ctl00%24ucMarketPlaceSupportNavigation%24btnSigninTop" => "Sign+in",
);
var_dump($postfields);
//I created the string myself for the post, else I got an error because we already encoded the variable names.
$p = "";
foreach($postfields as $k=>$v)
$p .= $k.'='.$v.'&';
//do the new post
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $p);
$ret = curl_exec($ch);//Get result after login page.
//this contains 'You have entered an invalid password' so it works as expected.
var_dump($ret);
?>
【讨论】:
rawurlencode()
将导致“此页面的状态信息无效,可能已损坏。”以上是关于无法使用cURL连接到HTTPS站点怎么办的主要内容,如果未能解决你的问题,请参考以下文章
Scrapy无法连接到仅支持旧版TLSv1的HTTPS站点。连接丢失