Php method for obtaining https requests using curl _ PHP Tutorial

Source: Internet
Author: User
Tags how to use curl
Php uses curl to obtain https requests. Php uses curl to obtain https requests. This article describes how php uses curl to obtain https requests. it involves curl's operation skills for https requests and is very useful, the required php method to use curl to obtain https requests

This article mainly introduces how php uses curl to obtain https requests. it involves curl's operation skills for https requests and is very useful. For more information, see

This example describes how php uses curl to obtain https requests. Share it with you for your reference. The specific analysis is as follows:

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: add the following content to the curl request:

The code is as follows:

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

Curl https request code

The code is as follows:

/** Curl to obtain https requests
* @ Param String $ url request url
* @ Param Array $ number of data to be sent
* @ Param Array $ header the header sent during the request
* @ Param int $ timeout Time, 30 s by default
*/
Function curl_https ($ url, $ data = array (), $ header = array (), $ timeout = 30 ){
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false); // skip certificate check
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, true); // check whether the SSL encryption algorithm exists from the certificate
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header );
Curl_setopt ($ ch, CURLOPT_POST, true );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, http_build_query ($ data ));
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 ';
$ Data = array ('name' => 'fdipzone ');
$ Header = array ();

$ Response = curl_https ($ url, $ data, $ header, 5 );

Echo $ response;
?>

I hope this article will help you with php programming.

This article describes how to use curl to obtain https requests in php. it involves the operation skills of curl for https requests and is very useful...

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.