4

I have been trying to post login credentials to an https site using cURL and PHP with no luck. Everything works fine for unsecured sites but I can't get it with https. I know the headers details that I am posting are correct (although I mocked them up for the sake of this example). Please help.

    <?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);

    ?>
Jake Sankey
  • 4,977
  • 12
  • 39
  • 53
  • 3
    Help with what? No code, no error messages, no nothing. I didn't pay the Psychic Hotline bill last month, so I can't read your mind right now... – Marc B Nov 29 '11 at 21:36
  • @MarcB: the original code contained his actual password so he made a quick edit. – Tom van der Woerdt Nov 29 '11 at 21:36
  • 1
    Yeah we need to at least know what `$content` or `$store` is when it fails. – drew010 Nov 29 '11 at 21:37
  • Try `if ($content === FALSE) { die (curl_error($ch)); }` after your curl_exec() calls, to see if there's something blowing up inside curl. – Marc B Nov 29 '11 at 21:39

2 Answers2

4
  1. Export the certificate.
  2. Upload it to where your script can see it.
  3. Then add:

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/path/to/certificateCA.crt");
    
djdy
  • 6,779
  • 6
  • 38
  • 62
1

I used this once to connect to a bank account, hope this helps:

$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);
Andrei
  • 1,183
  • 2
  • 19
  • 40
  • Downvote for `curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);` – djdy Nov 29 '11 at 21:53
  • Why, what does it do ? (I can connect using this to HSBC, Chase, and other banks) – Andrei Nov 29 '11 at 21:54
  • 1
    It will work, but it makes curl accept any server certificate. This means it does not check CA signatures, and whether they are trusted. – djdy Nov 29 '11 at 21:58