Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davy_yg's avatar
Level 27

stdClass::$results

Hello,

I get this error message:

Undefined property: stdClass::$results

RajaOngkir.php

	public static function get_province()
	{

	//
    $curl = curl_init();

	curl_setopt_array($curl, array(
	    CURLOPT_URL => "https://api.rajaongkir.com/starter/province",
	    CURLOPT_RETURNTRANSFER => true,
	    CURLOPT_ENCODING => "",
	    CURLOPT_TIMEOUT => 30000,
	    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	    CURLOPT_CUSTOMREQUEST => "GET",
	    CURLOPT_HTTPHEADER => array(
        "key: ". env('RAJAONGKIR_KEY')
        // 
        ),
	));

Suprising enough the result of dd(env('RAJAONGKIR_KEY')) is null

I already define RAJAONGKIR_KEY, I wonder why it is still null ?

ref: https://stackoverflow.com/questions/25417312/laravel-model-returns-undefined-property-stdclassname

0 likes
7 replies
Sinnbeck's avatar

You should never use env() outside of config files. It will return null as soon as you cache config

1 like
davy_yg's avatar
Level 27

Then, what's the alternative to pass the API key ?

Sinnbeck's avatar

@davy_yg use a config file. For instance if you add it to services.php

'raj' => env('RAJAONGKIR_KEY')
CURLOPT_HTTPHEADER => array(
        "key: ". config('services.raj')
        // 
        ),
1 like
davy_yg's avatar
Level 27

I tried this:

RajaOngkir.php

	public static function get_province()
	{

	//
    $curl = curl_init();

	curl_setopt_array($curl, array(
	    CURLOPT_URL => "https://api.rajaongkir.com/starter/province",
	    CURLOPT_RETURNTRANSFER => true,
	    CURLOPT_ENCODING => "",
	    CURLOPT_TIMEOUT => 30000,
	    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	    CURLOPT_CUSTOMREQUEST => "GET",
	    CURLOPT_HTTPHEADER => array(
        "key: ". config('services.rajaongkir_key')

        ),
	));

config/services.php

 'mailgun' => [
    	'domain' => env('MAILGUN_DOMAIN'),
    	'secret' => env('MAILGUN_SECRET'),
    	'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
	],

	'postmark' => [
    	'token' => env('POSTMARK_TOKEN'),
	],

	'ses' => [
   	 'key' => env('AWS_ACCESS_KEY_ID'),
    	'secret' => env('AWS_SECRET_ACCESS_KEY'),
    	'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
	],

	'rajaongkir_key' => env('RAJAONGKIR_KEY')

	];

"key: ". dd(config('services.rajaongkir_key')) equals to null

Looks like it only works if I put: "key: ". config('app.rajaongkir_key')

Why it doesn't works in services ? Is services the best place to put the rajaongkir_key ?

Sinnbeck's avatar

@davy_yg maybe you forgot a clear your cache (not sure why you cache config in the first place). I would put it in services as it sounds like an external service. But it's up to you

And the fact that the key already exists in app.php, makes me think this isn't your first time with this exact issue. Maybe make a note and put on your computer screen

I should never use env() outside of config files

Snapey's avatar

test with tinker

>>> config('services.rajaongkir_key')

easiest way to check config has the right values

Snapey's avatar

By the way, what has this to do with the question title?

Please or to participate in this conversation.