Created
February 6, 2024 18:47
-
-
Save ano/361474affa4b05d390be981d4064ccd5 to your computer and use it in GitHub Desktop.
PHP Birth Certificate code to create Birth Certificate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Include the web3.php library | |
require 'vendor/autoload.php'; | |
use Web3\Web3; | |
use Web3\Contract; | |
// Check if the request method is POST | |
if ($_SERVER['REQUEST_METHOD'] === 'POST') { | |
// Retrieve the data from the POST request | |
$recipient = $_POST['recipient']; | |
$childName = $_POST['childName']; | |
$dateOfBirth = $_POST['dateOfBirth']; | |
$placeOfBirth = $_POST['placeOfBirth']; | |
$sex = $_POST['sex']; | |
$parent1Name = $_POST['parent1Name']; | |
$parent2Name = $_POST['parent2Name']; | |
// Connect to an Ethereum node | |
$web3 = new Web3('https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID'); | |
// Define the contract ABI and address | |
$contractAbi = json_decode('[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"string","name":"childName","type":"string"},{"internalType":"uint256","name":"dateOfBirth","type":"uint256"},{"internalType":"string","name":"placeOfBirth","type":"string"},{"internalType":"string","name":"sex","type":"string"},{"internalType":"string","name":"parent1Name","type":"string"},{"internalType":"string","name":"parent2Name","type":"string"}],"name":"issueBirthCertificate","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"_birthCertificates","outputs":[{"internalType":"string","name":"childName","type":"string"},{"internalType":"uint256","name":"dateOfBirth","type":"uint256"},{"internalType":"string","name":"placeOfBirth","type":"string"},{"internalType":"string","name":"sex","type":"string"},{"internalType":"string","name":"parent1Name","type":"string"},{"internalType":"string","name":"parent2Name","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"_tokenCounter","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"}]'); | |
$contractAddress = 'CONTRACT_ADDRESS'; | |
// Create a contract instance | |
$contract = new Contract($web3->provider, $contractAbi); | |
$contract->at($contractAddress, function ($err, $instance) use ($recipient, $childName, $dateOfBirth, $placeOfBirth, $sex, $parent1Name, $parent2Name) { | |
if ($err !== null) { | |
echo "Error: " . $err; | |
return; | |
} | |
// Call the issueBirthCertificate function | |
$instance->issueBirthCertificate($recipient, $childName, $dateOfBirth, $placeOfBirth, $sex, $parent1Name, $parent2Name, function ($err, $result) { | |
if ($err !== null) { | |
echo "Error: " . $err; | |
return; | |
} | |
echo "Transaction hash: " . $result; | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment