How to use curl to get https request in php_PHP tutorial

WBOY
Release: 2016-07-13 10:07:08
Original
1111 people have browsed it

How to use curl to obtain https requests in PHP

This article mainly introduces the method of using curl to obtain https requests in PHP. It involves the operation skills of curl for https requests, which is very practical. Value, friends in need can refer to it

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

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 gets https request
* @param String $url requested url
* @param Array $data The data to be sent
* @param Array $header header sent when requesting
* @param int $timeout timeout, 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/956981.htmlTechArticleHow PHP uses curl to obtain https requests. This article mainly introduces how PHP uses curl to obtain https requests, involving Curl's operation skills for https requests are very practical and necessary...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template