<?php
function isValidSolanaAddressRPC(string $walletAddress): bool
{
// Solana RPC URL
$rpcUrl = "https://a...content-available-to-author-only...a.com";
// JSON-RPC payload for getAccountInfo
$payload = [
"jsonrpc" => "2.0",
"id" => 1,
"method" => "getAccountInfo",
"params" => [
$walletAddress, // Wallet address to validate
["encoding" => "jsonParsed"]
]
];
// Initialize cURL
'Content-Type: application/json',
]);
// Execute the request
// Check HTTP response
if ($httpCode !== 200) {
throw new Exception("Error: RPC request failed with HTTP status $httpCode.");
}
// Decode the JSON response
// Debug: Print the response (useful for troubleshooting)
// print_r($data);
// Check for a valid account in the response
if (isset($data['result']['value']) && $data['result']['value'] !== null) { return true; // Address is valid and exists on-chain
}
return false; // Address is invalid or not found
}
// Example usage
try {
$walletAddress = "4Nd1mQEjaF8mPyYNqcAySAnj7JxxR4pFdHRSj2ZXn7sX"; // Test wallet address
if (isValidSolanaAddressRPC($walletAddress)) {
echo "Valid Solana wallet address." . PHP_EOL;
} else {
echo "Invalid Solana wallet address." . PHP_EOL;
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . PHP_EOL;
}
PD9waHAKCmZ1bmN0aW9uIGlzVmFsaWRTb2xhbmFBZGRyZXNzUlBDKHN0cmluZyAkd2FsbGV0QWRkcmVzcyk6IGJvb2wKewogICAgLy8gU29sYW5hIFJQQyBVUkwKICAgICRycGNVcmwgPSAiaHR0cHM6Ly9hLi4uY29udGVudC1hdmFpbGFibGUtdG8tYXV0aG9yLW9ubHkuLi5hLmNvbSI7CgogICAgLy8gSlNPTi1SUEMgcGF5bG9hZCBmb3IgZ2V0QWNjb3VudEluZm8KICAgICRwYXlsb2FkID0gWwogICAgICAgICJqc29ucnBjIiA9PiAiMi4wIiwKICAgICAgICAiaWQiID0+IDEsCiAgICAgICAgIm1ldGhvZCIgPT4gImdldEFjY291bnRJbmZvIiwKICAgICAgICAicGFyYW1zIiA9PiBbCiAgICAgICAgICAgICR3YWxsZXRBZGRyZXNzLCAvLyBXYWxsZXQgYWRkc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