Home > Software design >  Notice: Trying to get property of non-object error PHP
Notice: Trying to get property of non-object error PHP

Time:11-30

I am new in PHP and trying to fetch IP information using API code like below

$ip = $_SERVER["REMOTE_ADDR"];

$proxycheck_options = array(
  'API_KEY' => '######', // Your API Key.
  'ASN_DATA' => 1, // Enable ASN data response.
  'DAY_RESTRICTOR' => 7, // Restrict checking to proxies seen in the past # of days.
  'VPN_DETECTION' => 1, // Check for both VPN's and Proxies instead of just Proxies.
  'RISK_DATA' => 1, // 0 = Off, 1 = Risk Score (0-100), 2 = Risk Score & Attack History.
  'INF_ENGINE' => 1, // Enable or disable the real-time inference engine.
  'TLS_SECURITY' => 0, // Enable or disable transport security (TLS).
  'QUERY_TAGGING' => 1, // Enable or disable query tagging.
  'CUSTOM_TAG' => '', // Specify a custom query tag instead of the default (Domain Page).
  'BLOCKED_COUNTRIES' => array('Wakanda', 'CN'), // Specify an array of countries or isocodes to be blocked.
  'ALLOWED_COUNTRIES' => array('Azeroth', 'US') // Specify an array of countries or isocodes to be allowed.
);
  
$result_array = \proxycheck\proxycheck::check($ip, $proxycheck_options);
echo "<pre>";
print_r($result_array);
echo "</pre>";

echo $result_array[0]->status;

print_r giving me response like this

Array
(
    [status] => ok
    [node] => LETO
    [157.32.X.X] => Array
        (
            [asn] => AS55836
            [provider] => Reliance Jio Infocomm Limited
            [continent] => Asia
            [country] => India
            [isocode] => IN
            [region] => Gujarat
            [regioncode] => GJ
            [city] => Ahmedabad
            [latitude] => 23.0276
            [longitude] => 72.5871
            [proxy] => no
            [type] => Business
            [risk] => 0
        )

    [block] => no
    [block_reason] => na
)

I am interested in getting status, provider and proxy value from it but its giving me error like

PHP Notice:  Trying to get property 'status' of non-object in

anyone here can please help me how to properly I can get the value from this type array?

Thanks!

CodePudding user response:

As per the information you've shown us,$result_array doesn't have a 0 index (let alone one which contains an object with properties).

The print_r results show us that it's an associative array, and therefore you can simply access the "status" property directly as an index of the array:

echo $result_array["status"];

There's more info and examples in the PHP manual page on arrays and in many other places online.

  • Related