在 PHP 中解析解码 JSON(区块链 API)

Parsing Decoded JSON in PHP (blokchain API)

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

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

这个 link 允许从一个地址获取所有比特币交易。 全是引号,看不清楚

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

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

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

在此先感谢您,如果您能做到这一点,您将真正帮助我!

如果您的目标是显示您要执行的所有 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 output

$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>";