![](/img/trans.png)
[英]Metamask (web3) connect wallet and send transaction - how to change blockchain to Bianance smart chain (BEP-20) network instead of Ethereum?
[英]Store ethereum transaction id (metamask) in php variable
我不習慣 javascript,我正在使用下面的代碼通過元掩碼發送以太坊交易,看起來一切正常,我可以在使用 document.write 發送交易后打印交易的 hash,但是不能'找不到一種方法將 hash 存儲在 php 變量中,以便之后在數據庫中處理它等等。
我需要 php 變量中“txHash”的值。 非常感謝一些幫助! 謝謝
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 在瀏覽器 JavaScript之前執行。它無法與 MetaMask 交互,因為它無法訪問擴展的window.ethereum
object。
但是,您可以使用 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,
})
})
由於x-www-form-urlencoded
內容類型,交易 hash 現在在 PHP 腳本的$_POST["hash"]
變量中可用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.