-1

I have never used cURL before and I think I have hit a roadblock in my learning. I am trying to make a HTTP GET request to my Wowza server which uses the Rest API to return JSON results. The URL actually returns it in XML but Wowza support says it I can get the response in JSON by adding the content type as I have done.

$url = 'http://DOMAINNAME:8087/v2/servers/_defaultServer_/vhosts/_defaultVHost_/applications/live/instances/_definst_/incomingstreams/ncopeland';

 $cURL = curl_init();

 curl_setopt($cURL, CURLOPT_URL, $url);
 curl_setopt($cURL, CURLOPT_HTTPGET, true);

curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
     'Content-Type: application/json;charset=utf-8',
     'Accept: application/json'
 ));

 $result = curl_exec($cURL);
 $result = json_decode($result,true);

curl_close($cURL);

The response should be this.

{
  "serverName": "_defaultServer_",
  "sourceIp": "ncopeland",
  "isPTZEnabled": false,
  "applicationInstance": "_definst_",
  "name": "ncopeland",
  "isRecordingSet": false,
  "isStreamManagerStream": true,
  "isPublishedToVOD": false,
  "isConnected": true,
  "ptzPollingInterval": 2000
}

But, instead the response is being returned and formatted like this.

{"serverName":"_defaultServer_","sourceIp":"ncopeland","isPTZEnabled":false,"applicationInstance":"_definst_","name":"ncopeland","isRecordingSet":false,"isStreamManagerStream":true,"isPublishedToVOD":false,"isConnected":false,"ptzPollingInterval":2000}

How can I format this so I can get these into usable variables. Really all I am needing from the response is "name" and "isConnected" so I can updated fields in a DB. Really all I am needing from the response is "name" and "isConnected" so I can updated fields in a DB like this.

Array (
[serverName] => _defaultServer_
[sourceIp] => ncopeland
[isPTZEnabled] => false
[applicationInstance] => _definst_
[name] => ncopeland
[isRecordingSet] => false
[isStreamManagerStream] => true
[isPublishedToVOD] => false
[isConnected] => false
[ptzPollingInterval] => false

)

So I can work with $obj variable as an array like so.

echo $obj['name'];
echo $obj['isConnected'];
Texan78
  • 687
  • 2
  • 16
  • 42

2 Answers2

1

I'm not sure what are you missing, everything should be working as intended from your code. The fact, that json returns data without newline, doesn't change how data will be used later.

$jsondata = '{"serverName":"_defaultServer_","sourceIp":"ncopeland","isPTZEnabled":false,"applicationInstance":"_definst_","name":"ncopeland","isRecordingSet":false,"isStreamManagerStream":true,"isPublishedToVOD":false,"isConnected":false,"ptzPollingInterval":2000}';

$result = json_decode($jsondata,true); //is array
var_dump ($result['serverName']);
var_dump ($result['isConnected']);

Besides that, keep in mind, that you cannot echo boolean values.

BTW, using objects is even simpler than arrays in my opinion.

$jsondata = '{"serverName":"_defaultServer_","sourceIp":"ncopeland","isPTZEnabled":false,"applicationInstance":"_definst_","name":"ncopeland","isRecordingSet":false,"isStreamManagerStream":true,"isPublishedToVOD":false,"isConnected":false,"ptzPollingInterval":2000}';
$result = json_decode($jsondata);


var_dump ($result->serverName);
var_dump ($result->isConnected);
Sapnix
  • 55
  • 1
  • 8
0

There was nothing wrong with my code after all. Come to find out on my hosted server solution the package I was using from Bluehost blocked the ports I was needing to make the http request via cURL. I was able to upgrade to a dedicated IP to get the ports I was needing open so it would work and has been working great since.

Texan78
  • 687
  • 2
  • 16
  • 42