fork download
  1. <?php
  2.  
  3. function isValidSolanaAddressRPC(string $walletAddress): bool
  4. {
  5. // Solana RPC URL
  6. $rpcUrl = "https://a...content-available-to-author-only...a.com";
  7.  
  8. // JSON-RPC payload for getAccountInfo
  9. $payload = [
  10. "jsonrpc" => "2.0",
  11. "id" => 1,
  12. "method" => "getAccountInfo",
  13. "params" => [
  14. $walletAddress, // Wallet address to validate
  15. ["encoding" => "jsonParsed"]
  16. ]
  17. ];
  18.  
  19. // Initialize cURL
  20. $ch = curl_init($rpcUrl);
  21. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  22. curl_setopt($ch, CURLOPT_POST, true);
  23. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  24. 'Content-Type: application/json',
  25. ]);
  26. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
  27.  
  28. // Execute the request
  29. $response = curl_exec($ch);
  30. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  31. curl_close($ch);
  32.  
  33. // Check HTTP response
  34. if ($httpCode !== 200) {
  35. throw new Exception("Error: RPC request failed with HTTP status $httpCode.");
  36. }
  37.  
  38. // Decode the JSON response
  39. $data = json_decode($response, true);
  40.  
  41. // Debug: Print the response (useful for troubleshooting)
  42. // print_r($data);
  43.  
  44. // Check for a valid account in the response
  45. if (isset($data['result']['value']) && $data['result']['value'] !== null) {
  46. return true; // Address is valid and exists on-chain
  47. }
  48.  
  49. return false; // Address is invalid or not found
  50. }
  51.  
  52. // Example usage
  53. try {
  54. $walletAddress = "4Nd1mQEjaF8mPyYNqcAySAnj7JxxR4pFdHRSj2ZXn7sX"; // Test wallet address
  55. if (isValidSolanaAddressRPC($walletAddress)) {
  56. echo "Valid Solana wallet address." . PHP_EOL;
  57. } else {
  58. echo "Invalid Solana wallet address." . PHP_EOL;
  59. }
  60. } catch (Exception $e) {
  61. echo "Error: " . $e->getMessage() . PHP_EOL;
  62. }
Success #stdin #stdout 0.03s 26472KB
stdin
Standard input is empty
stdout
Error: Error: RPC request failed with HTTP status 0.