[英]Store ethereum transaction id (metamask) in php variable
I'm not used to javascript and I am using the code below to send an ethereum transaction via metamask, and it looks like it is all working fine, I can print the transaction's hash after it is sent with document.write, but can't find a way to store the hash in a php variable to afterwards deal with it in a database and whatever.我不习惯 javascript,我正在使用下面的代码通过元掩码发送以太坊交易,看起来一切正常,我可以在使用 document.write 发送交易后打印交易的 hash,但是不能'找不到一种方法将 hash 存储在 php 变量中,以便之后在数据库中处理它等等。
I need the value of 'txHash' in a php variable.我需要 php 变量中“txHash”的值。 Would appreciate a lot some help!
非常感谢一些帮助! Thanks
谢谢
sendEthButton.addEventListener('click', () => {
ethereum
.request({
method: 'eth_sendTransaction',
params: [
{
from: accounts[0],
to: '< - RECEIVING ACCOUNT - >',
value: '0xDE0B6B3A7640000',
},
],
})
.then((txHash) => document.write(txHash))
.catch((error) => console.error);
});
PHP is executed before the browser JavaScript. It's not able to interact with MetaMask, as it doesn't have access to the window.ethereum
object of the extension. PHP 在浏览器 JavaScript之前执行。它无法与 MetaMask 交互,因为它无法访问扩展的
window.ethereum
object。
However, you can use JavaScript fetch() function to send the transaction ID (or any other data) from JS to a PHP script.但是,您可以使用 JavaScript fetch() function 将事务 ID(或任何其他数据)从 JS 发送到 PHP 脚本。
ethereum.request({
// ...
}).then((txHash) => {
fetch('/txIdReceiver.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: 'hash=' + txHash,
})
})
Because of the x-www-form-urlencoded
content type, the transaction hash is now available in the $_POST["hash"]
variable of the PHP script.由于
x-www-form-urlencoded
内容类型,交易 hash 现在在 PHP 脚本的$_POST["hash"]
变量中可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.