Currently im trying to build an explorer and im stuck on the block/listing transactions.
Currently this is my code.
foreach($info['tx'] AS $key => $value){
$txinfo = callBTC("getrawtransaction",array("\"$value\"",true));
echo '<pre>';
print_r($txinfo);
echo '</pre>';
echo '<div class="tx"><div class="txRow"><div class="txCat">Hash</div><div class="txInfo"><div class="overflow"><a href="index?txid='.$value.'">'.$value.'</a></div></div></div>';
echo '<div class="txRow"><div class="txCat">Time</div><div class="txInfo">'.date("Y-m-d H:i:s",$txinfo['time']).'</div></div>';
echo '<div class="txRow"><div class="txCat">From</div><div class="txInfo"><div class="overflow">';
foreach($txinfo['vin'] AS $key2 => $value2){
if(!empty($txinfo['vin'][$key2]['coinbase'])){
echo '<div class="confirmed">COINBASE - Newly Created Coins</div>';
}else{
$from = callBTC("getrawtransaction",array("\"$value2[txid]\"",true));
echo '<pre>';
print_r($from);
echo '</pre>';
echo $from['vout'][0]['scriptPubKey']['addresses'][0].'<br/>';
}
}
echo '</div></div></div>';
echo '<div class="txRow"><div class="txCat">To</div><div class="txInfo"><div class="overflow">';
foreach($txinfo['vout'] AS $key3 => $value3){
if(!empty($value3['scriptPubKey']['addresses'])){
foreach($value3['scriptPubKey']['addresses'] AS $key4 => $value4){
echo '<a href="index?addid='.$value4.'">'.$value4.'</a><br/>';
}
}
}
echo '</div></div></div>';
echo '<div class="txRow"><div class="txCat">Fee</div><div class="txInfo"></div></div>';
echo '<div class="txRow"><div class="txCat">Amount</div><div class="txInfo"></div></div></div>';
}
i got the to addresses fine but whne it comes to the from field i struggle to determine which is the valid address to use.
Lets use block 2092235 for example
I use blockchain.com to check against my site to ensure im displaying the proper data.
but when i spit out the transaction for the tx id ac709fc580430d51ab741e339edd184c349306c9881f95085851eef0652b1fb6 i get this...
Array
(
[txid] => ac709fc580430d51ab741e339edd184c349306c9881f95085851eef0652b1fb6
[hash] => b388c750f3d15f1fcdb320fbc3398a65a34a3eb6933ca1738729f22a4a8a5924
[version] => 2
[size] => 222
[vsize] => 141
[weight] => 561
[locktime] => 2092234
[vin] => Array
(
[0] => Array
(
[txid] => 122e916b2236fb1d6c5d21e886ffe72b3834ca3d81bed51686aa91d5c069ed5c
[vout] => 1
[scriptSig] => Array
(
[asm] =>
[hex] =>
)
[txinwitness] => Array
(
[0] => 304402201c4ff3fa6d7d212760db2c03e39bc824756c10bbac77a10db7665c383e16600b0220146f62d2c0a28d335ecad0f38227259ead389d409259f3af693628443695bbf401
[1] => 0258471daabd690819d6cbb34b8ca75f98ba24604960301ce7c90cc5131ab07280
)
[sequence] => 4294967294
)
)
[vout] => Array
(
[0] => Array
(
[value] => 0.01905871
[n] => 0
[scriptPubKey] => Array
(
[asm] => 0 2cda63f440de6ab858cf2550daf3b25522d6c66f
[hex] => 00142cda63f440de6ab858cf2550daf3b25522d6c66f
[reqSigs] => 1
[type] => witness_v0_keyhash
[addresses] => Array
(
[0] => tb1q9ndx8azqme4tskx0y4gd4uaj253dd3n0386xkn
)
)
)
[1] => Array
(
[value] => 72.44723495
[n] => 1
[scriptPubKey] => Array
(
[asm] => 0 e179ad3cb0d3bc53cba564cc2e05bc2dc18de1ab
[hex] => 0014e179ad3cb0d3bc53cba564cc2e05bc2dc18de1ab
[reqSigs] => 1
[type] => witness_v0_keyhash
[addresses] => Array
(
[0] => tb1qu9u6609s6w798ja9vnxzupdu9hqcmcdtvm87gx
)
)
)
)
[hex] => 020000000001015ced69c0d591aa8616d5be813dca34382be7ff86e8215d6c1dfb36226b912e120100000000feffffff02cf141d00000000001600142cda63f440de6ab858cf2550daf3b25522d6c66f27b5d1af01000000160014e179ad3cb0d3bc53cba564cc2e05bc2dc18de1ab0247304402201c4ff3fa6d7d212760db2c03e39bc824756c10bbac77a10db7665c383e16600b0220146f62d2c0a28d335ecad0f38227259ead389d409259f3af693628443695bbf401210258471daabd690819d6cbb34b8ca75f98ba24604960301ce7c90cc5131ab07280caec1f00
[blockhash] => 000000000000003f69d7c8cb4943df2d7a273883eb72024f3305122ed2dc7271
[confirmations] => 1418
[time] => 1631039838
[blocktime] => 1631039838
)
then i get the tx id from the vin field to get info on the transaction for that address(the from field) and i get this.
Array
(
[txid] => 122e916b2236fb1d6c5d21e886ffe72b3834ca3d81bed51686aa91d5c069ed5c
[hash] => 5fb52307d771c7dadf9f06006340a0acb5ff9ebe15d215d98f08e749d0ee2e59
[version] => 2
[size] => 222
[vsize] => 141
[weight] => 561
[locktime] => 2092232
[vin] => Array
(
[0] => Array
(
[txid] => 2739adc2b59de66a9e707ec3e102549dc56a8432e38ef471cb961976a27739be
[vout] => 0
[scriptSig] => Array
(
[asm] =>
[hex] =>
)
[txinwitness] => Array
(
[0] => 3044022077216db352b3024fbb92ff798b618dc3a45fe727077872d2ee5e699ea8fc839d02207dde0f94b0d2cd8900dda6c189134c5d7614278a582e2e1f131c0e16be86534e01
[1] => 036e8d75e18796979cdfebcc82d44930d5265ad3820156932194bd7cda5192296a
)
[sequence] => 4294967294
)
)
[vout] => Array
(
[0] => Array
(
[value] => 0.01275647
[n] => 0
[scriptPubKey] => Array
(
[asm] => 0 184337352372a5fa6218dd57ea1057305d39c77d
[hex] => 0014184337352372a5fa6218dd57ea1057305d39c77d
[reqSigs] => 1
[type] => witness_v0_keyhash
[addresses] => Array
(
[0] => tb1qrppnwdfrw2jl5cscm4t75yzhxpwnn3mamh6ff8
)
)
)
[1] => Array
(
[value] => 72.46645714
[n] => 1
[scriptPubKey] => Array
(
[asm] => 0 c4520316f8d1dee0a19acde950d0540701d3d013
[hex] => 0014c4520316f8d1dee0a19acde950d0540701d3d013
[reqSigs] => 1
[type] => witness_v0_keyhash
[addresses] => Array
(
[0] => tb1qc3fqx9hc680wpgv6eh54p5z5quqa85qn35meep
)
)
)
)
[hex] => 02000000000101be3977a2761996cb71f48ee332846ac59d5402e1c37e709e6ae69db5c2ad39270000000000feffffff02ff76130000000000160014184337352372a5fa6218dd57ea1057305d39c77dd209efaf01000000160014c4520316f8d1dee0a19acde950d0540701d3d01302473044022077216db352b3024fbb92ff798b618dc3a45fe727077872d2ee5e699ea8fc839d02207dde0f94b0d2cd8900dda6c189134c5d7614278a582e2e1f131c0e16be86534e0121036e8d75e18796979cdfebcc82d44930d5265ad3820156932194bd7cda5192296ac8ec1f00
[blockhash] => 00000000000000d299298de1cfb14276667b7d55e8a1cf43c9dc825babadf058
[confirmations] => 1420
[time] => 1631039256
[blocktime] => 1631039256
)
by looking at blockchain i know which address it SHOULD take (tb1qc3fqx9hc680wpgv6eh54p5z5quqa85qn35meep)...but when i use php and check for last key in array its not always the correct address in other transactions as sometimes the address i need is in a different index(same array tho just different index).
How can i programmatically determine the right address consistently?