0

I have run the below code

$api_key = 'XXXXXXXXX';
$api_url = "http://sms.eduapp.com:8381/app/miscapi/".$api_key."/getBalance/true/";
$credit_balance = file_get_contents( $api_url);
echo $credit_balance;

And got output

   [{"ROUTE_ID":"101057","ROUTE":"SMSWAY_TRANS","BALANCE":"13700"}]

From the above output I just want to get value 13700 in php variable.

Thanks in advance :)

sandeep autade
  • 261
  • 7
  • 17
  • 1
    use json_decode as `$array = json_decode('[{"ROUTE_ID":"101057","ROUTE":"SMSWAY_TRANS","BALANCE":"13700"}]'); echo $array[0]->BALANCE ;` – Devsi Odedra Jan 18 '20 at 07:38

2 Answers2

1

the site return you an array try this

$api_key = 'XXXXXXXXX';
$api_url = "http://sms.eduapp.com:8381/app/miscapi/".$api_key."/getBalance/true/";
$credit = file_get_contents( $api_url);
// sorry im forgot to decode th json
$credit_balance = json_decode($credit, true);
echo $credit_balance['BALANCE'];
hosein in jast
  • 370
  • 2
  • 15
1
$credit_balance = file_get_contents($api_url);
$balance = json_decode($credit_balance, true);
echo $balance[0]['BALANCE'];

Or:

$credit_balance = file_get_contents($api_url);
$balance = json_decode($credit_balance);
echo $balance[0]->BALANCE
jeprubio
  • 17,312
  • 5
  • 45
  • 56