简体   繁体   English

注意:试图获取非对象错误 PHP 的属性

[英]Notice: Trying to get property of non-object error PHP

I am new in PHP and trying to fetch IP information using API code like below我是 PHP 的新手,并尝试使用 API 代码获取 IP 信息,如下所示

$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 print_r 给我这样的回应

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!谢谢!

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).根据您向我们展示的信息, $result_array没有0索引(更不用说包含具有属性的 object 的索引了)。

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: print_r结果表明它是一个关联数组,因此您可以直接访问“status”属性作为数组的索引:

echo $result_array["status"];

There's more info and examples in the PHP manual page on arrays and in many other places online. arrays 上的 PHP 手册页和许多其他在线位置有更多信息和示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM