Annie Khalid Annie Khalid - 1 month ago 6
PHP Question

How to access object in php

i am accessing object property with following code

$address = $_POST['address'];
$balance = file_get_contents('https://block.io/api/v2/get_address_balance/?api_key=xxxx&addresses='. $address);
echo $balance->data->available_balance;
Print_r($balance);


Actually i want to echo available_balance and output of print_r is this

{
"status" : "success",
"data" : {
"network" : "BTC",
"available_balance" : "0.00000000",
"pending_received_balance" : "0.00000000",
"balances" : [
{
"user_id" : null,
"label" : null,
"address" : "xxxxxxxxxxxxxxxxxxxxxxxxx",
"available_balance" : "0.00000000",
"pending_received_balance" : "0.00000000"
}
]
}


the next part of this question is please suggest me how to protect from SQl Injection Attack, in my project all forms are submitted through ajax, if user can sent only 20 requests to database in a day from one IP, is it will be good in SQL injection Protection.

Answer

I'm assuming this is a JSON data, so you've to decode it as PHP object first. Try the following code.

$balance = json_decode($balance);

echo $balance->data->balances[0]->available_balance