skip to Main Content

I want to write a little webapp that will show some data. The data comes from an API. All data from the API I get displayed, but if I only want to display specific data, I always get an error.

I have not had much experience with API’s yet, but all the tutorials I’ve done show the data via this echo method.

My current code:

<?php 
$user_id = "3bb8d3bc-ab7a-45fb-8154-ed54897b2c4c";
$api_response = file_get_contents('https://r6tab.com/api/player.php?p_id='.$user_id);
$api_response_decoded = json_decode($api_response, true);

//foreach($api_response_decoded as $val) {

    //echo $val, '<br>';

//}

echo $api_response_decoded->p_name;

?>

api_response Output:

"playerfound":true,"social":{"twitter":"","instagram":"","mixer":"","twitch":"","youtube":"","bio":"","esl":"","discord":"","background":"","aliases":"","embed":""},"seasonal":{"current_NA_mmr":0,"last_NA_mmr":0,"last_NA_mmrchange":0,"current_EU_mmr":3831,"last_EU_mmr":3881,"last_EU_mmrchange":-50,"current_AS_mmr":0,"last_AS_mmr":0,"last_AS_mmrchange":0,"total_casualwins":6,"total_casuallosses":5,"total_casualtotal":11,"total_casualkills":28,"total_casualdeaths":18,"total_rankedwins":38,"total_rankedlosses":24,"total_rankedtotal":62,"total_rankedkills":244,"total_rankeddeaths":201,"total_generalwins":44,"total_generallosses":29,"total_generaltotal":73,"total_generalkills":272,"total_generaldeaths":219,"total_totalbulletshits":3851,"total_totalhs":172,"total_totaltimeplayed":0,"bomb_wins":67,"bomb_losses":41,"bomb_total":108,"secure_wins":0,"secure_losses":0,"secure_total":0,"hostage_wins":1,"hostage_losses":0,"hostage_total":1,"favorite_mode":"bomb"},"matches":[{"casual_wlstatus":"won","casual_winslost":"1 Won","casual_datatime":"10/02/19","ranked_wlstatus":"won","ranked_winslost":"6 Won, 2 Lost","ranked_datatime":"10/02/19","next":"default","db_p_total_casualwins":1,"db_p_total_casuallosses":0,"db_p_total_casualkills":2,"db_p_total_casualdeaths":2,"db_p_total_rankedwins":6,"db_p_total_rankedlosses":2,"db_p_total_rankedkills":23,"db_p_total_rankeddeaths":23,"db_p_total_totalhs":10,"db_p_NA_currentmmr":0,"db_p_EU_currentmmr":3881,"db_p_AS_currentmmr":0,"NA_mmrchange":0,"EU_mmrchange":189,"AS_mmrchange":0},

My Error Message:

Trying to get property 'p_name' of non-object in C:xampphtdocsR6S-Statsstats.php on line 12

2

Answers


  1. By using the true flag on json_decode, you’re creating an associated array instead of an object. Remove that flag, and you’ll be able to use the object methods:

    $api_response_decoded = json_decode($api_response);
    echo $api_response_decoded->p_name; //Scenus
    

    If you want to keep it as an associated array, you can access the data like this:

    echo $api_response_decoded['p_name'];
    
    Login or Signup to reply.
  2. when use json_decode(‘dsfsdf’, true);

      $p_name = $api_response_decoded['p_name'];
    

    when use json_decode(‘dsfsdf);

    $p_name = $api_response_decoded->p_name;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search