Curl Get HTTPS Request method

Source: Internet
Author: User
Tags curl header http request ssl certificate

Today in doing a project, need to curl get Third-party API, the other side of the API is HTTPS way.

Before using curl to get an HTTP request, the following error occurred while obtaining the HTTPS request today: Certificate validation failed.

SSL certificate problem, verify the CA cert is OK. Details:error:14090086:ssl routines:SSL3_GET_SERVER_CERTIFICATE:certificate Verify failed

Workaround, when Curl request, join

curl_setopt ($ch, Curlopt_ssl_verifypeer, false); Skip certificate checking  
curl_setopt ($ch, Curlopt_ssl_verifyhost, true);  Check to see if the SSL encryption algorithm exists from the certificate

Curl HTTPS Request Code

<?php/** Curl Obtain HTTPS request * @param String $url The requested URL * @param Array $header the header * @param sent when requested  
    NT $timeout timeout time, default 30s/function Curl_https ($url, $header =array (), $timeout =30) {$ch = Curl_init (); curl_setopt ($ch, Curlopt_ssl_verifypeer, false);  Skip certificate Checking curl_setopt ($ch, Curlopt_ssl_verifyhost, true);  
    Check the SSL encryption algorithm for the existence of curl_setopt ($ch, Curlopt_url, $url) from the certificate;  
    curl_setopt ($ch, Curlopt_httpheader, $header);  
    curl_setopt ($ch, Curlopt_post, true);  
    curl_setopt ($ch, Curlopt_postfields, "");   
    curl_setopt ($ch, Curlopt_returntransfer, true);  
      
    curl_setopt ($ch, Curlopt_timeout, $timeout);  
      
    $response = curl_exec ($ch);  
    if ($error =curl_error ($ch)) {die ($error);  
      
    } curl_close ($ch);  
      
return $response;  
}//Call $url = ' https://www.example.com/api/message.php ';  
      
$header = Array (' Content-type:application/json '); $responsE = Curl_https ($url, $header, 5);  
Echo $response; ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.