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 about how php uses curl to obtain https requests, see the following example. 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.
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:
<? Php
/** 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.