CURL Login to HTTPS

I am trying to send credentials to login to https using cURL and PHP with no luck. Everything works fine for insecure sites, but I can't get it using https. I know that the header data that I publish is correct (although I mocked them 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 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); ?> 
+4
source share
2 answers
  • Certificate Export.
  • Download it to where your script can see it.
  • 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"); 
+4
source

I used this time 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); 
+1
source

Source: https://habr.com/ru/post/1383738/


All Articles