0

您好我正在尝试从 json 格式页面中提取一些信息

该页面是:https ://blockchain.info/fr/rawaddr/1BQLNJtMDKmMZ4PyqVFfRuBNvoGhjigBKF

此链接允许从一个地址获取所有比特币交易。所有的引号都把我弄糊涂了,我看不清楚

我想像原来的区块链网站一样显示一切 Blockchain Display view 一开始会是这样的

$json = file_get_contents("https://blockchain.info/fr/rawaddr/1BQLNJtMDKmMZ4PyqVFfRuBNvoGhjigBKF");
var_dump(json_decode($json));

我可以将基本信息从 JSO 提取到 php,但是这里有太多事务,我认为我们需要使用循环来显示所有内容,但我不知道该怎么做如果有人可以在 php 中为我显示第 5 个交易它会很同情。

非常感谢你如果你能做到这一点,你会真的帮助我!

4

1 回答 1

0

如果您的目标是显示您想要执行的所有 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>";
于 2017-07-08T11:06:04.727 回答