cURL登录HTTPS站点

时间:2011-11-29 21:34:12

标签: php curl

我一直在尝试使用cURL和PHP将登录凭据发布到https网站,但没有运气。对于不安全的网站,一切正常,但我无法通过https获取。我知道我发布的标题详细信息是正确的(虽然我为了这个例子而嘲笑它们)。请帮忙。

    <?php
    // Initialize cURL
    $ch = curl_init('https://secured-example.com/auth.asp');

    // Enable HTTP POST
    curl_setopt($ch, CURLOPT_POST, 1);

    // Use SSL 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);

    // Set POST parameters
    curl_setopt($ch, CURLOPT_POSTFIELDS, 'username=myUser&password=myPass');

    // Imitate classic browser's behavior - handle cookies
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    // Execute 1st request
    $store = curl_exec($ch);

    // Set file to download
    curl_setopt($ch, CURLOPT_URL, 'https://secured-example.com/file.pdf');

    // Execute 2nd request (file download)
    $content = curl_exec($ch);

    curl_close($ch);

    ?>

2 个答案:

答案 0 :(得分:4)

  1. 导出证书。
  2. 将其上传到脚本可以看到的位置。
  3. 然后添加:

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/path/to/certificateCA.crt");
    

答案 1 :(得分:1)

我曾使用过一次连接到银行帐户,希望这会有所帮助:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, HSBC_LINK1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);

$post_data = array('post1' => 'value');

$fields_string = '';
foreach($post_data as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } 
$fields_string = rtrim($fields_string,'&');
curl_setopt($ch, CURLOPT_POST, count($post_data)); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);

curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$data1 = curl_exec($ch);