How to get file_get_contents() to work with HTTPS?

PhpCurlHttpsFile Get-Contents

Php Problem Overview


I'm working on setting up credit card processing and needed to use a workaround for CURL. The following code worked fine when I was using the test server (which wasn't calling an SSL URL), but now when I am testing it on the working server with HTTPS, it's failing with the error message "failed to open stream".

function send($packet, $url) {
  $ctx = stream_context_create(
    array(
      'http'=>array(
        'header'=>"Content-type: application/x-www-form-urlencoded",
        'method'=>'POST',
        'content'=>$packet
      )
    )
  );
  return file_get_contents($url, 0, $ctx);
}

Php Solutions


Solution 1 - Php

To allow https wrapper:

  • the php_openssl extension must exist and be enabled
  • allow_url_fopen must be set to on

In the php.ini file you should add this lines if not exists:

extension=php_openssl.dll

allow_url_fopen = On

Solution 2 - Php

Try the following script to see if there is an https wrapper available for your php scripts.

$w = stream_get_wrappers();
echo 'openssl: ',  extension_loaded  ('openssl') ? 'yes':'no', "\n";
echo 'http wrapper: ', in_array('http', $w) ? 'yes':'no', "\n";
echo 'https wrapper: ', in_array('https', $w) ? 'yes':'no', "\n";
echo 'wrappers: ', var_export($w);

the output should be something like

openssl: yes
http wrapper: yes
https wrapper: yes
wrappers: array(11) {
  [...]
}

Solution 3 - Php

$url= 'https://example.com';

$arrContextOptions=array(
      "ssl"=>array(
            "verify_peer"=>false,
            "verify_peer_name"=>false,
        ),
    );  

$response = file_get_contents($url, false, stream_context_create($arrContextOptions));

This will allow you to get the content from the url whether it is a HTTPS

Solution 4 - Php

Try the following.

function getSslPage($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

> Note: This disables SSL verification, meaning the security offered > by HTTPS is lost. Only use this code for testing / local > development, never on the internet or other public-facing > networks. If this code works, it means the SSL certificate isn't > trusted or can't be verified, which you should look into fixing as a > separate issue.

Solution 5 - Php

This is probably due to your target server not having a valid SSL certificate.

Solution 6 - Php

Just add two lines in your php.ini file.

extension=php_openssl.dll

allow_url_include = On

its working for me.

Solution 7 - Php

HTTPS is supported starting from PHP 4.3.0, if you have compiled in support for OpenSSL. Also, make sure the target server has a valid certificate, the firewall allows outbound connections and allow_url_fopen in php.ini is set to true.

Solution 8 - Php

I had the same error. Setting allow_url_include = On in php.ini fixed it for me.

Solution 9 - Php

use the following code:

$homepage = file_get_contents("https://www.google.com",false,
                             stream_context_create([
                                'ssl'  => [
                                    'verify_peer'      => false,
                                    'verify_peer_name' => false,
                                ]
                            ])
            );
    
 echo $homepage;

Solution 10 - Php

In my case, the issue was due to WAMP using a different php.ini for CLI than Apache, so your settings made through the WAMP menu don't apply to CLI. Just modify the CLI php.ini and it works.

Solution 11 - Php

Sometimes a server will choose not to respond based on what it sees or doesn't see in the http request headers (such as an appropriate user agent). If you can connect with a browser, grab the headers it sends and mimic them in your stream context.

Solution 12 - Php

I was stuck with non functional https on IIS. Solved with:

file_get_contents('https.. ) wouldn't load.

  • download https://curl.haxx.se/docs/caextract.html

  • install under ..phpN.N/extras/ssl

  • edit php.ini with:

    curl.cainfo = "C:\Program Files\PHP\v7.3\extras\ssl\cacert.pem" openssl.cafile="C:\Program Files\PHP\v7.3\extras\ssl\cacert.pem"

finally!

Solution 13 - Php

TO CHECK IF AN URL IS UP OR NOT

The code in your question can be rewritten as to a working version:

function send($packet=NULL, $url) {
// Do whatever you wanted to do with $packet
// The below two lines of code will let you know if https url is up or not
$command = 'curl -k '.$url;
return exec($command, $output, $retValue);
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJames SimpsonView Question on Stackoverflow
Solution 1 - PhphassanView Answer on Stackoverflow
Solution 2 - PhpVolkerKView Answer on Stackoverflow
Solution 3 - PhpDushyant DagarView Answer on Stackoverflow
Solution 4 - PhpBenjamin CrouzierView Answer on Stackoverflow
Solution 5 - PhpEmil VikströmView Answer on Stackoverflow
Solution 6 - PhpRitesh ChandoraView Answer on Stackoverflow
Solution 7 - PhpGordonView Answer on Stackoverflow
Solution 8 - PhpJim JonesView Answer on Stackoverflow
Solution 9 - PhpHojjatView Answer on Stackoverflow
Solution 10 - PhpdtbarneView Answer on Stackoverflow
Solution 11 - PhpGZippView Answer on Stackoverflow
Solution 12 - PhpTesonView Answer on Stackoverflow
Solution 13 - PhpCommon ManView Answer on Stackoverflow