如果您的目标是显示您想要执行的所有 Tx 信息。
$json = file_get_contents("http://blockchain.info/fr/rawaddr/1BQLNJtMDKmMZ4PyqVFfRuBNvoGhjigBKF");
$txs = json_decode($json,1)['txs'];
echo"<pre>"; //just to get a human readable display
foreach($txs as $txinfo){
echo"A new tx";
//will display the full tx data
print_r($txinfo);
}
echo"</pre>";
请注意,区块链交易可能有点复杂,因为一个交易可以有多个输入和多个输出。您可能还希望循环通过输出以显示从交易中接收比特币的所有地址,在这种情况下,您可以简单地添加另一个 foreach 输出
$json = file_get_contents("http://blockchain.info/fr/rawaddr/1BQLNJtMDKmMZ4PyqVFfRuBNvoGhjigBKF");
$txs = json_decode($json,1)['txs'];
echo"<pre>"; //just to get a human readable display
foreach($txs as $txinfo){
echo"A new tx";
//will display the full tx data
print_r($txinfo);
// will cycle trough all output for each tx
foreach ($txinfo['out'] as $outgoingTransaction)
{
//Will display the receiving address
$receivingAddress = $outgoingTransaction['addr'];
//will get the amount sent in satoshis
$receivingAmountInSatoshi = $outgoingTransaction['value'];
echo"<br>1BQLNJtMDKmMZ4PyqVFfRuBNvoGhjigBKF sent to $receivingAddress $receivingAmountInSatoshi satoshis <br>";
}
}
添加 tx 理解逻辑的更高级代码
$walletAddress = '1AWKFrvFYuCC7ef2m2zX73pWu1C15FRGjR' ;
$json = file_get_contents("http://blockchain.info/fr/rawaddr/$walletAddress");
$txs = json_decode($json,1)['txs'];
echo"<pre>"; //just to get a human readable display
foreach($txs as $txinfo){
$spendingTx = false ;
$totalSpent = 0 ;
$totalReceived = 0;
echo"<p>Txid = $txinfo[hash]<br>";
//print_r($txinfo);
// we need to find out if the address is the sender or the receiver
$senderData = reset($txinfo['inputs']); //using reset to get only the first input
if ($senderData['prev_out']['addr'] ==$walletAddress ){
//the address is the sender meaning the address is spending
$spendingTx = true ;
}
//it's a spend tx then we cycle trough receivers
if ($spendingTx) {
foreach ($txinfo['out'] as $outgoingTransaction) {
//Will display the receiving address
$receivingAddress = $outgoingTransaction['addr'];
//will get the amount sent in satoshis
$receivingAmountInSatoshi = $outgoingTransaction['value'];
$totalSpent = $totalSpent + $receivingAmountInSatoshi ;
echo "<br>$walletAddress sent to $receivingAddress $receivingAmountInSatoshi satoshis <br>";
}
echo "<br>Total spent = $totalSpent" ;
}
//it is not a spending tx so it's a receceiving tx
else {
foreach ($txinfo['out'] as $outgoingTransaction) {
//We keep only receiving data concerning current wallet
if ($outgoingTransaction['addr'] == $walletAddress) {
//Will display the receiving address
$receivingAddress = $outgoingTransaction['addr'];
//will get the amount sent in satoshis
$receivingAmountInSatoshi = $outgoingTransaction['value'];
$senderAddress = $senderData['prev_out']['addr'];
$totalReceived = $receivingAmountInSatoshi;
echo "<br>$walletAddress received $receivingAmountInSatoshi satoshis from $senderAddress<br>";
}
}
echo "<br>Total received = $totalReceived" ;
}
echo"<br>end tx </p>";
}
echo"</pre>";