Notice: Trying to get property of non-object error

Notice: Trying to get property of non-object error

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Decoding the Trying to get property of non-object Error in PHP

Encountering a Notice: Trying to get property or offset of non-object error is a classic frustration for developers working with APIs and unstructured data. It usually signals that your code expects an object, but instead received something else entirely—often null. In the specific case of fetching player data from api.convoytrucking.net, this error stems from a mismatch between how you are decoding the JSON response and how you are accessing the resulting structure.

When you execute file_get_contents() to retrieve the raw API response, you get a string containing JSON text. The critical step is json_decode(). By default, PHP's json_decode() returns an associative array when given no flags, but it returns an object of type stdClass only if you explicitly pass the second argument as true. Without this flag, $pjs becomes a plain array. Consequently, attempting to access $pjs->{'player_name'} fails because arrays use square brackets for property-like access in PHP syntax ($pjs['player_name']), whereas the arrow operator (->) is reserved for objects. Attempting to use -> on an array triggers the notice you are seeing.

Fixing the Decoding Logic

The immediate fix is to modify your decoding call to ensure a standard class object is returned. Update your code to:

$js = file_get_contents('http://api.convoytrucking.net/api.php?api_key=public&show=player&player_name=Mick_Gibson');
$pjs = json_decode($js, true); // The second argument 'true' is crucial here
// Now access using array syntax or recreate as object if needed
echo $pjs['player_name']; 

Alternatively, if you prefer to work with objects for dot-notation access later in the pipeline, you can pass false (or omit the flag) but must ensure you use bracket notation consistently: $pjs['player_name']. However, looking at the var_dump($pjs) output provided in your source material reveals a nested structure. The root element is an array containing a single item at index [0], which holds the actual stdClass object with the player details.

Handling Nested Structures

The real culprit here isn't just the missing true flag for decoding; it's also how you are indexing the result. Your var_dump shows:

array(1) { [0]=> object(stdClass)#52 ... }

This means $pjs is an array with one element, not a direct object. To access player_name, your code must account for this nesting. You need to access the first element of the array before accessing the property:

var_dump($pjs[0]->{'player_name'}); 
// OR if you decoded with true:
var_dump($pjs[0]['player_name']);

If you had decoded with true, $pjs would be an array, so $pjs['0'] or $pjs[0] is required to reach the object inside. If you decode without the flag and want object syntax, you must navigate: $pjs[0]->player_name.

Best Practices for API Integration

When integrating third-party APIs like Convoy Trucking, relying on file_get_contents is functional but lacks error handling. If the network times out or returns an HTTP 4xx/5xx status, file_get_contents might return an empty string. Passing an empty string to json_decode() results in null. Attempting to access a property on that null value will throw a fatal error rather than just a notice, potentially crashing your script.

A more robust approach involves checking the HTTP status code before decoding:

$response = file_get_contents('http://api.convoytrucking.net/api.php?api_key=public&show=player&player_name=Mick_Gibson');

if ($response === false) {
    // Handle network failure
    echo "Failed to fetch data.";
    exit;
}

$data = json_decode($response, true);

if (json_last_error() !== JSON_ERROR_NONE) {
    // Handle malformed JSON
    echo "Invalid JSON response: " . json_last_error_msg();
    exit;
}

// Check if the structure exists before accessing properties
if (!empty($data[0]['player_name'])) {
    echo $data[0]['player_name'];
} else {
    echo "No player data found.";
}

This pattern ensures you don't crash on unexpected empty responses. For Laravel applications, this logic is often encapsulated within a custom Model or Service class to keep controllers clean. If you are maintaining a legacy codebase where file_get_contents is entrenched, understanding the distinction between decoding into an array versus an object and respecting the nested array index in the response is the key to resolving this specific notice.

For further reading on handling API responses efficiently within the Laravel ecosystem, resources like https://laravelcompany.com offer insights into building robust service layers that abstract these raw HTTP interactions.

Our network