1

Im trying to use API. I get data coded in JSON in array.

Now I need to encode the array to get the value I need, but don't know exactly how to do it. I want to get pln value.

This is what I get:

object(stdClass)#1 (2) { 
    ["status"]=> string(2) "ok" 
    ["data"]=> object(stdClass)#2 (4) {   
        ["pln"]=> string(6) "0.0000" 
        ["usd"]=> string(6) "0.0000" 
        ["btc"]=> string(10) "0.00000000" 
        ["eur"]=> string(6) "0.0000" 
    } 
}

Orginal API code:

function getBalance($url){
    $ch = curl_init();

    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    $output = curl_exec($ch);

    curl_close($ch);
    return $output;
}

$url = $main_api_url.'balance/'.$api_user_id.'/'.$api_key.'/'.$api_secret.'/';

var_dump(
json_decode(
    getBalance($url)
)
);

This is my code:

function getBalance($url){
    $ch = curl_init();

    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    $output = curl_exec($ch);

    curl_close($ch);
    return $output;
}

$url = $main_api_url.'balance/'.$api_user_id.'/'.$api_key.'/'.$api_secret.'/';

$json = file_get_contents($url);

$data = json_decode($json, TRUE);

print "$data[pln]";

How I can get exactly pln value? Im not interested in whole array.

1
  • Can you put <pre> before var_dump $data ? Not sure but i think u can access 'pln' like this $data->data->pln Commented Nov 15, 2014 at 14:31

1 Answer 1

1

For the modified version, since it returns an array, you can point it directly:

echo $data['data']['pln'];

So finally:

function getBalance($url) {
    $ch = curl_init();

    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    $output = curl_exec($ch);

    curl_close($ch);
    return $output;
}

$url = $main_api_url.'balance/'.$api_user_id.'/'.$api_key.'/'.$api_secret.'/';
$json = file_get_contents($url);
$data = json_decode($json, true);

echo $data['data']['pln'];
       //   ^ its inside data
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.