I tried this simple code :
<?php
$publicKey = openssl_pkey_get_public("public.pem");
$plaintext = "String to encrypt";
openssl_public_encrypt($plaintext, $encrypted, $publicKey);
echo $encrypted; //encrypted string
?>
but I get this error message.
Warning: openssl_public_encrypt(): key parameter is not a valid public key in C:\xampp\htdocs\encrypt\encrypt_rsa.php on line 6