使用curl

时间:2016-03-13 01:26:03

标签: php curl https

我正在尝试使用curl登录https://www.investagrams.com/login,它只显示空白页面。我刚接触你,所以请耐心等待。有人可以指出我的代码中的问题是什么。

<!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>

1 个答案:

答案 0 :(得分:2)

我发现你的代码出现了一些问题:

  1. 您需要返回第二个卷曲请求的值。
  2. 如果您使用curl从已经包含在这些标记中的网站下载信息,则无需声明DOCTYPE或使用<html><body>标记。
  3. 正如@Terminus在对您的问题的评论中提到的那样,您需要在第一个网址的末尾添加.com
  4. 在这些修订之后,您的代码应为:

    <?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_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);,所发生的情况是,它不会验证服务器提供的SSL证书,允许冒充任何服务器curl连接到。

    此外,由于https://investagrams.com拥有有效的SSL证书,因此您没有理由将CURLOPT_SSL_VERIFYPEER设置为false。