简体   繁体   English

在 php 变量中存储以太坊交易 ID(metamask)

[英]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.

相关问题 Metamask (web3) 连接钱包并发送交易 - 如何将区块链更改为 Bianance 智能链 (BEP-20) 网络而不是以太坊? - Metamask (web3) connect wallet and send transaction - how to change blockchain to Bianance smart chain (BEP-20) network instead of Ethereum? 使用 ReactJS 和 Javascript 中的 Metamask 连接用户以太坊帐户 - Connecting User Ethereum account with Metamask in ReactJS and Javascript 未在 android 的 Metamask 应用程序上检测到以太坊提供商 - Not detecting ethereum provider on Metamask App for android 无法检索 MetaMask 账户的以太坊余额 - Unable to retrieve the Ethereum balance of MetaMask account 如何在javascript变量中存储作为id传递给javascript的php变量 - how to store php variable passed as id to javascript in a javascript variable 使用单个交易处理 2 个以太坊交易 - Process 2 Ethereum Transactions with single Transaction Web3 元掩码连接无法读取未定义的属性(读取“以太坊”) - Web3 metamask connection Cannot read properties of undefined (reading 'ethereum') Metamask 移动浏览器未在 android 中注入 window.ethereum - Metamask mobile browser not injecting window.ethereum in android MetaMask - RPC 错误:参数无效:必须提供以太坊地址 - MetaMask - RPC Error: Invalid parameters: must provide an Ethereum address 如何在Metamask扩展中处理拒绝交易 - How to handle reject transaction in metamask extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM