2

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
  • Why don't you print out $obj to see what is stored in that variable? Use print_r() as it shows a nice organised result. Commented Nov 23, 2016 at 9:47
  • I already know what is stored. The issue is checking if the object is available. Commented Nov 23, 2016 at 9:58

2 Answers 2

2

How about this?

$streamStatus = (isset($obj->isConnected) && $obj->isConnected)?'true':'false';
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, isset that is what I was looking for but wasn't sure how to apply it in this situation. Let me test this and I will report back but, I think this should do the trick.
1

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 Comment

You make a very valid point regarding using isset(). I will give this a try as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.