This example describes how PHP uses curl to obtain HTTPS requests. Share to everyone for your reference. The specific analysis is as follows:
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
The workaround is to join when the Curl request:
Copy Code code as follows:
curl_setopt ($ch, Curlopt_ssl_verifypeer, false); Skipping 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
Copy Code code as follows:
<?php
/** Curl Get HTTPS request
* @param String $url the requested URL
* @param Array $data The data to be sent
* The header to send when @param Array $header request
* @param int $timeout timeout time, default 30s
*/
function Curl_https ($url, $data =array (), $header =array (), $timeout =30) {
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_ssl_verifypeer, false); Skipping certificate checking
curl_setopt ($ch, Curlopt_ssl_verifyhost, true); Check to see if 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 your PHP program design.