How can SSL encryption be integrated into PHP APIs to protect data transmission between clients and servers?
To integrate SSL encryption into PHP APIs to protect data transmission between clients and servers, you can use the cURL library to make secure HTTPS requests. By setting the appropriate cURL options, you can ensure that the communication between the client and server is encrypted using SSL/TLS protocols.
$url = 'https://api.example.com';
$data = ['key1' => 'value1', 'key2' => 'value2'];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$response = curl_exec($ch);
if($response === false){
echo 'cURL error: ' . curl_error($ch);
} else {
echo $response;
}
curl_close($ch);