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

正确处理“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 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作