dsij89625 2016-11-23 09:44
浏览 3902
已采纳

正确处理“Undefined property:stdClass ::”

Here is my issue, I am aware why this is happening but, I am unsure how to resolve it as my research on this issue hasn't returned anything valid to my certain issue.

I am making a cURL request and parsing the objects to get their value and then storing it in a variable to insert into a DB later. The HTTP request is from a streaming server. With that said if the stream is not active the object is not present but, if the object is present it is returned like so...

<IsConnected>true</IsConnected>

So in my script I have been parsing the object like so to find if the value of the object is true or false then storing that value in a variable to insert into a DB.

$streamStatus = $obj->isConnected?'true':'false';

While this works great, it is not correct syntax because when the object is not present due to the stream not being active then I get the following errors in my error log by the dozens.

Undefined property: stdClass::$isConnected

So my question is, how can I can I properly address this so I can get it's value of true when it is active and the object is present but false when the object is not present?

EDIT

Here is the full response if the stream is active and what is returned by the request.

<IncomingStream serverName="_defaultServer_">
<ApplicationInstance>_definst_</ApplicationInstance>
<Name>ncopeland</Name>
<SourceIp>rtmp://xx.xxx.xx.xx:xxxxxx</SourceIp>
<IsRecordingSet>false</IsRecordingSet>
<IsStreamManagerStream>false</IsStreamManagerStream>
<IsPublishedToVOD>false</IsPublishedToVOD>
<IsConnected>true</IsConnected>
<IsPTZEnabled>false</IsPTZEnabled>
<PtzPollingInterval>2000</PtzPollingInterval>
</IncomingStream>
  • 写回答

2条回答 默认 最新

  • duanchen6423 2016-11-23 09:50
    关注

    While this works great, it is not correct syntax because when the object is not present

    Then simply add the check for object validity prior interacting with it:

    $streamStatus = (is_object($obj) && property_exists($obj, 'isConnected') && $obj->isConnected) ? 'true' : 'false';
    

    NOTE that other answers may suggest using isset() for the above but this is wrong as isset() is not correct tool for the task due to the fact how it handles null.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里