title: How to mutual/two-way authenticate with php curl?
body: <p>The website is saying "You are not authenticated with digital certificate, or possibly the connection time expired. Authenticate again with your digital certificate.".</p>
  
  <p>I have gotten this message every time, so rather than connection time expiring, I think that my attempt of authenticating is just not working.</p>
  
  <p>The way I am doing this is I start with just a pfx file, and a passphrase to open it. I make the pem certificate with openssl and the pfx.</p>
  
  <p>Code:</p>
  
  <pre><code>&lt;?php
  $pfx_path = 'me.pfx';
  $pfx_pass = 'foopass123';
  $pfx_values = [];
  openssl_pkcs12_read(file_get_contents($pfx_path), $pfx_values, $pfx_pass);
  $cert_path = 'me.pem';
  file_put_contents($cert_path, $pfx_values['cert'] . "\n" . $pfx_values['pkey']);
  $postfields = 'field1=val1&amp;field2=val2&amp;field3=val3';
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL           , 'https://www.example.com');
  curl_setopt($ch, CURLOPT_POST          , true);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS    , $postfields);
  curl_setopt($ch, CURLOPT_SSLCERT       , $cert_path);
  curl_setopt($ch, CURLOPT_VERBOSE, 1);
  $curl_result = curl_exec($ch);
  $curl_info = curl_getinfo($ch);
  curl_close($ch);
  </code></pre>
  
  <p>Result of executing the code (showing just a quote of the HTML of $curl_result): </p>
  
  <blockquote>
    <p>You are not authenticated with digital certificate, or possibly the
    connection time expired. Authenticate again with digital certificate.</p>
  </blockquote>
  
  <p>By the way, <code>$curl_info['ssl_verify_result']</code> is 0, which I guess means that at least the server was correctly authenticated.</p>
  
  <p><strong>EDIT:</strong> removed <code>CURLOPT_SSLCERTPASSWD</code> because it's not necessary, the pfx file was already open with the password, and its private and public key were put naked into <code>$cert_path</code>.</p>