Home  >  Article  >  Backend Development  >  How to use curl to get https request in php, phpcurl gets https_PHP tutorial

How to use curl to get https request in php, phpcurl gets https_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:06:47673browse

php uses curl to get https request method, phpcurl gets https

The example in this article describes how PHP uses curl to obtain https requests. Share it with everyone for your reference. The specific analysis is as follows:

I am working on a project today and need to use curl to obtain a third-party API. The other party’s API is https.
I was able to obtain http requests using curl before, but when I obtained https requests today, the following error message appeared: 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

The solution is to add:
during curl request

Copy code 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

Copy code The code is as follows:
/**curl gets https request
* @param String $url The requested url
* @param Array $data The data to be sent
* @param Array $header The header sent when requesting
* @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); // 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 be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/957142.htmlTechArticlephp uses curl to obtain https requests, phpcurl obtains https. This article describes how php uses curl to obtain https requests. . Share it with everyone for your reference. The specific analysis is as follows: Today...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn