在PHP中解析解码的JSON(Blokchain API) [英] Parsing Decoded JSON in PHP (blokchain API)

查看:117
本文介绍了在PHP中解析解码的JSON(Blokchain API)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hello I am trying to extract some info from a json format page

该页面为: https://blockchain.info/fr/rawaddr/1BQLNJtMDKmMZ4PyqVF

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));

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

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 !

推荐答案

如果您的目标是显示所有Tx信息,则需要这样做.

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

请注意,由于一项交易可以具有多个输入和多个输出,因此区块链交易可能有点复杂. 您可能还希望循环通过低谷输出以显示从交易中收到比特币的所有地址 在这种情况下,您只需添加另一个foreach输出

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

    }

}

添加tx理解逻辑的更高级的代码

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

这篇关于在PHP中解析解码的JSON(Blokchain API)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆