问题
Hello I am trying to extract some info from a json format page
The page is : https://blockchain.info/fr/rawaddr/1BQLNJtMDKmMZ4PyqVFfRuBNvoGhjigBKF
this link allows to get all bitcoin transactions from an address. All the quotation marks confuse me, I can not see clearly
I want to display everything like the original blockchain website Blockchain Display view The beginning will be something like that
$json = file_get_contents("https://blockchain.info/fr/rawaddr/1BQLNJtMDKmMZ4PyqVFfRuBNvoGhjigBKF");
var_dump(json_decode($json));
I can extract basic info from JSO to php, but here there is too much transactions and I think we need to use a loop to display everything but I don't know how to do that If someone can display for me in php the 5 first transactions it will be very sympathic.
Thanks a lot in advance youw ill really help me if you can do that !
回答1:
If you goal is to display all Tx info you want to do that.
$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>";
Note a blockchain transaction can be a little complexe since one transaction can have multiple input and multiple outputs. you might want to cycle trough outputs as well to display all addresses who received bitcoin from a transaction in that case you can simply add another 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>";
    }
}
a more advanced code to add tx understanding logic
$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>";
    来源:https://stackoverflow.com/questions/44980979/parsing-decoded-json-in-php-blokchain-api