Php 使用curl登录https
我尝试使用curl登录,它只显示空白页面。顺便说一句,我是新手,请容忍我。有人能指出我代码中的问题吗Php 使用curl登录https,php,curl,https,Php,Curl,Https,我尝试使用curl登录,它只显示空白页面。顺便说一句,我是新手,请容忍我。有人能指出我代码中的问题吗 <!DOCTYPE html> <html> <body> <?php // Create temp file to store cookies $ckfile = tempnam ("/tmp", "CURLCOOKIE"); // URL to login page $url = "https://www.investagrams.com/lo
<!DOCTYPE html>
<html>
<body>
<?php
// Create temp file to store cookies
$ckfile = tempnam ("/tmp", "CURLCOOKIE");
// URL to login page
$url = "https://www.investagrams.com/login";
// Get Login page and its cookies and save cookies in the temp file
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
$fields = array(
'ctl00$WelcomePageMainContent$ctl00$Username' => '******',
'ctl00$WelcomePageMainContent$ctl00$Password' => '******',
);
$fields_string = '';
foreach($fields as $key=>$value) {
$fields_string .= $key . '=' . $value . '&';
}
rtrim($fields_string, '&');
// Post login form and follow redirects
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$output = curl_exec($ch);
$url = "https://www.investagrams.com/Stock/RealTimeMonitoring";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
?>
</body>
</html>
我注意到您的代码有一些地方有问题:
或
标记.com
<?php
// Create temp file to store cookies
$ckfile = tempnam ("/tmp", "CURLCOOKIE");
// URL to login page
$url = "https://www.investagrams.com/login";
// Get Login page and its cookies and save cookies in the temp file
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // Accepts all CAs
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
$fields = array(
'ctl00$WelcomePageMainContent$ctl00$Username' => '******',
'ctl00$WelcomePageMainContent$ctl00$Password' => '******',
);
$fields_string = '';
foreach($fields as $key=>$value) {
$fields_string .= $key . '=' . $value . '&';
}
rtrim($fields_string, '&');
// Post login form and follow redirects
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // Accepts all CAs
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$output = curl_exec($ch);
$url = "https://www.investagrams.com/Stock/RealTimeMonitoring";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // Accepts all CAs
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
echo $output;
?>
编辑:
正如@Tom在下面的评论中所说,您不应该使用curl\u setopt($ch,CURLOPT\u SSL\u VERIFYPEER,false)代码>除非绝对必要,否则它不会验证服务器提供的SSL证书,从而允许模拟任何服务器连接
另外,,由于具有有效的SSL证书,因此您没有理由将CURLOPT_SSL\u VERIFYPEER
设置为false。您的第一个url似乎缺少.com
您需要在第二次声明后返回$output的值。我尝试删除第一个$output并回显第二个$output,但仍然返回空白页@C.Liddellhanks bro,但仍然返回空白页@terminus您也不需要在代码中添加DOCTYPE声明、html或body标记,因为这将随curl请求一起返回。哇!这很有效,兄弟,谢谢你。你救了我很多钱time@C.Liddell您应该添加一个关于CURLOPT_SSL_VERIFYPEER的警告,因为它具有严重的安全隐患。@Tom Good catch。对问题添加了通知。