Curl is used to obtain the https request method, curlhttps

Source: Internet
Author: User

Curl is used to obtain the https request method, curlhttps

Curl


In a project today, curl is required to obtain a third-party API. The API of the other party is in https mode.

Previously, you can use curl to obtain an http request. However, when you obtain an https request today, the following error message is displayed: Certificate verification failed.

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

Solution, Added to the curl request

Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false); // skip the certificate check curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, true); // check from the certificate whether the SSL encryption algorithm exists

Curl https Request Code

<? Php/** curl Get https Request * @ param String $ url request url * @ param Array $ header sent during the request * @ param int $ timeout time, 30 s by default */function curl_https ($ url, $ header = array (), $ timeout = 30) {$ ch = curl_init (); curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false); // skip the certificate check curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, true); // check from the certificate whether the SSL encryption algorithm has curl_setopt ($ ch, CURLOPT_URL, $ url ); curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ hea Der); 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'response response Header = array ('content-type: application/json'); $ response = Curl_https ($ url, $ header, 5); echo $ response;?>



Curl https

Save the url by using session or other methods on the page of the callback address. You can call it on the curl page.

Php uses curl to obtain the https address

You even
Neither comment 1 nor comment 2 can output data.
Because it outputs your data by default.
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // The output content is a string
Add the above sentence.
In this way, no data is output.

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.