How to get file_get_contents() to work with HTTPS?

后端 未结 12 1954
长发绾君心
长发绾君心 2020-11-21 23:38

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 cal

相关标签:
12条回答
  • 2020-11-22 00:32

    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.

    0 讨论(0)
  • 2020-11-22 00:33
    $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

    0 讨论(0)
  • 2020-11-22 00:33

    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.

    0 讨论(0)
  • 2020-11-22 00:33

    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);
    }
    
    0 讨论(0)
  • 2020-11-22 00:35

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

    0 讨论(0)
  • 2020-11-22 00:42

    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!

    0 讨论(0)
提交回复
热议问题