title: MAMP & PHP “SSL operation failed with code 1”
body: <p><strong>EDIT</strong>
  I’m on the move at the moment, and only have SO on the Stack Exchange app on my iPhone, so there’s some weird formatting with the quotes in the code below - Apologies! I have real ones in the real code :)</p>
  
  <p>I’ve been trying to figure this out for two days with some other questions on SO, but here goes...</p>
  
  <p>Just trying to use <code>file_get_contents()</code> to capture the webpage of another file located on the same server and same domain, and include that. I’m using MAMP with a self signed cert for production (so that I can have the server force SSL etc) so there’s that, and I also have that certificate as “Always Trusted” locally on my Mac as it’s self-signed obviously.</p>
  
  <p>So now I have the issue where I want one page to capture the contents of another.. I initially tried using cURL and it was failing without giving me any exceptions, and no information using <code>curl_error()</code> so I switched over to <code>file_get_contents()</code> where I get the exception <code>SSL operation failed with code 1</code>... <code>ssl3_get_server_certificate:certificate verify failed</code></p>
  
  <p>I assume this is in issue with OpenSSL not trusting the self-signed (but I thought it used the trusted CA’s of the underlying OS?) and I can’t get it to work using the following stream context:</p>
  
  <pre><code>stream_context_create([
     "ssl" =&gt; [
        "allow_self_signed" =&gt; true
     ]
  ]);
  </code></pre>
  
  <p>And if I set <code>verify_peer</code> and <code>verify_peer_name</code> to false, the request is made but HTTPS cookies aren’t sent which breaks the whole thing.</p>
  
  <p>I’ve tried adding the actual text of the cert to the <code>cacert.pem</code> file under the OpenSSL directory in MAMP, and set that file in the <code>openssl.cafile</code> option in the <code>php.ini</code> file as stated in another answer, but alas no luck...</p>
  
  <p>Any ideas? Your help would be much appreciated! Thank you! ☺️</p>
  
  <p><strong>EDIT 2</strong>
  So I tried it again using cURL, and this time got cURL to give me some verbose output to a file, and this is what it gives me (note the line third from the bottom):</p>
  
  <pre><code>*   Trying ::1...
  * TCP_NODELAY set
  * Connected to admin.voyagerisc (::1) port 8890 (#0)
  * ALPN, offering http/1.1
  * Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
  * successfully set certificate verify locations:
  *   CAfile: /Applications/MAMP/Library/OpenSSL/certs/cacert.pem   CApath: none
  * SSL certificate problem: unable to get local issuer certificate
  * Curl_http_done: called premature == 1
  * Closing connection 0
  </code></pre>
  
  <p>...and this is the file where I have added the raw certificate to the top.. ugh, STUCK! </p>