Jump to content

Make HTTP GET request with cURL from Wowza Rest API


Go to solution Solved by Texan78,

Recommended Posts

I have never used cURL before and 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. I have authentication set to none on the server at the moment until I can get the script working. I have tested the code below with an external website and it works but, when I try to use it with the URL from the server with the Rest API it just times out. I can make the request in a browser fine, just not from the code. What am I missing or not doing correctly? 
 
    
$ch = curl_init("http://IP_TO_SERVER:8087/v2/servers/_defaultServer_/vhosts/_defaultVHost_/applications/live/instances/_definst_/incomingstreams/ncopeland"); // such as http://example.com/example.xml
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          curl_setopt($ch, CURLOPT_HEADER, 0);
          
    $data = curl_exec($ch);
    
    curl_close($ch);


    print $data;
 
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
}

-Thanks!

Yes I was and I was thinking the same as you and did some research and it turned out that it was a firewall issue on my web hosts side. Apparently Bluehost blocks all ports but the important ones unless you have a Dedicated IP. So I got a dedicated IP and now I am able to make the requests but still having a small issue trying to get what I am needing. 

 

I am able to make the request and return a response but, it isn't formatting like I am needing it. 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);

curl_close($cURL);

$obj = json_decode($result);     
   echo $obj->name;
   echo $obj->isConnected;

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 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'];

The formatting doesn't matter - those two JSON samples are equivalent.

 

$obj is an object by default. You can work with that just fine. It doesn't have to be an array. But if you really want an array then check the documentation that Barand linked to.

As requinx indicated, the two responses are the same.  It doesn't matter if it is on one line or multiple.  If you wanted on multiple lines for a human to better read it, but <pre> tags around it.  Also, I would personally keep it as an object, but if you really want, you can make it an array.

$result = curl_exec($cURL); //Returns string

// why not keep it as an object?
$obj = json_decode($result); 
echo('<pre>'.print_r($obj,1).'</pre>');
echo $obj->name;
echo $obj->isConnected;

// If you really want an array for some unknown reason OPTION 1

$obj = json_decode($result); 
$array= (array) $obj;
echo('<pre>'.print_r($array,1).'</pre>');

echo $obj['name'];
echo $obj['isConnected'];

// If you really want an array for some unknown reason OPTION 2
$array= json_decode($result,true); 
echo('<pre>'.print_r($array,1).'</pre>');

echo $obj['name'];
echo $obj['isConnected'];

Thanks for the input. It doesn't have to be an array, I would rather keep it as an object. I just figured if it was in an array it would be easier to work with when it came to inserting the value into the DB. I would rather keep it as an object but wasn't sure how to work with it as an object. 

 

Something isn't right though. I don't know if it has to do with just getting a dedicated IP and propagation isn't fully completed or what but, the code isn't parsing correctly. It's ether that or there is something I am missing in my code. 

 

This is currently what I have and I have tried many different combinations and tried many numerous things to even list. Did var_dump and it's not returning correctly. The original response is in XML and formatted like so.

<IncomingStream serverName="_defaultServer_">
<ApplicationInstance>_definst_</ApplicationInstance>
<Name>ncopeland</Name>
<SourceIp>ncopeland</SourceIp>
<IsRecordingSet>false</IsRecordingSet>
<IsStreamManagerStream>true</IsStreamManagerStream>
<IsPublishedToVOD>false</IsPublishedToVOD>
<IsConnected>false</IsConnected>
<IsPTZEnabled>false</IsPTZEnabled>
<PtzPollingInterval>2000</PtzPollingInterval>
</IncomingStream> 

This is my current code. 

$url = 'http://domain_name: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);
     
     curl_close($cURL);
    
// Lets get and parse the data and create the variables

     $obj = json_decode($result); 
     echo('<pre>'.print_r($obj,1).'</pre>');
     echo $obj->name;
     echo $obj->isConnected; 

This is the output I get from the above. 
 

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

It's like it's not doing anything. Maybe it has something to do with the <IncomingStream serverName="_defaultServer_"> and I am just not doing something right. Not sure where to start to begin troubleshooting. 

 

-Thanks

If your intention is to add the entire reply to a DB, I think you will be better off going as an array.  Use the $array= json_decode($result,true); approach.  If you just wanted a few values, you can use either approach, but objects are a little more concise.

 

Are you sure that is the output you are getting?

I think I took the CURLOPT_RETURNTRANSFER out by accident trying different things since I am still learning cURL and what the different options do experimenting so I know what they do so I know in the future. I have added it back but, I am still getting the same output. I even cleared my browser cache and restarted my computer since I did just get a dedicated IP last night just in case that is playing into it. 

 

This is currently what I have right now. 

$url = 'http://domain.com: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_RETURNTRANSFER, true);
     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);
     
     curl_close($cURL);
    
// Lets get and parse the data and create the variables

     $obj = json_decode($result); 
     echo('<pre>'.print_r($obj,1).'</pre>');
     echo $obj->name;
     echo $obj->isConnected;

And this is what I am getting in return. 

 

http://stream.dfwstormforce.com/inc/chasers/test.php

 

I don't need the entire response just the two values. I am only going to use the name to compare it to the name field in the DB so I can insert the isConnected value to the right user. I may not even go that route at all. May just insert the variable from the response directly into the code and if I go that route I just need the one value but, I have to do that for 6 other files too so thought it may be easier to call them all globally from a single file and update and insert them into the DB all at once since the variable is already connected to the DB I am using on the final page and I know it works because I can change the true/false in the database manually and it updates the icon which it is controlling via jQuery. Just trying to get this API response connect to it one way so that it is fully dynamic and will update when the status changes. 

 

-Thanks

If I understand correctly according to this http://php.net/json_decode the JSON response I am getting isn't proper formatted json. 

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

According to the manual it should be like this if I understand it correctly. 

{"serverName":_defaultServer_ "sourceIp":ncopeland "isPTZEnabled":false }

The value isn't suppose to be wrapped in single or double quotes and trailing commas are not allowed if I understand that correctly or maybe that is just when it is used in the code?

Something is different...

 

https://www.tehplayground.com/X1l6fB03lteHFYF2

 

 

You're absolutely right and I agree. When I inserted the string direct into a varible like that I comes out correctly. 

 

When I pull it from the this string..

 $result = curl_exec($cURL);
     
     curl_close($cURL);
    
// Lets get and parse the data and create the variables

     $obj = json_decode($result);

Then I get a different output. So I am not sure if my cURL OPTS are set correctly or if $result = curl_exec($cURL); is causing some issues. When a do a var dump on $cURL though it returns NULL if that helps or means anything. 

Edited by Texan78

What does this provide:

<?php
$url = 'http://domain.com: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_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_HTTPGET, true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json;charset=utf-8',
'Accept: application/json'
));

echo('make sure I am viewing the correct script');
$result = curl_exec($cURL);
var_dump($result);
exit;
Edited by NotionCommotion

Still getting the same output. I will leave it be and you can check that link I posted earlier. Something is wacky with it just not sure what. 

$cURL = curl_init();

curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_HTTPGET, true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json;charset=utf-8',
'Accept: application/json'
));

echo('make sure I am viewing the correct script');
$result = curl_exec($cURL);
var_dump($result);
exit;
    
// Lets get and parse the data and create the variables

     /* $obj = json_decode($result); 
     echo('<pre>'.print_r($obj,1).'</pre>');
     echo $obj->name;
     echo $obj->isConnected; */

I even tried it with adding this to see if I would get any errors and still the same output and no errors. 

$result = curl_exec($cURL);
     
     // Check for errors and display the error message
if($errno = curl_errno($cURL)) {
    $error_message = curl_strerror($cURL);
    echo "cURL error ({$errno}):\n {$error_message}";
}
     
     curl_close($cURL);

I am scratching my head. I know I don't know much about cURL but I think I am understanding it right, it's just not getting formatted correctly on the output and not sure why. Could be be something with my CURLOPT?

 

-Thanks!

Are you sure you are viewing the script that you think you are?  If it doesn't display "make sure I am viewing the correct script", then you are not.

 

 

Yes, I am getting the "make sure I am viewing the correct script" so I am viewing the correct one. 

 

You can see that here...

 

http://stream.dfwstormforce.com/inc/chasers/test.php

 

Which is using this code minus the including the URL variable for brevity. 

$cURL = curl_init();

curl_setopt($cURL, CURLOPT_URL, $url);

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

echo('make sure I am viewing the correct script');
$result = curl_exec($cURL);
var_dump($result);
exit;

I did run the full code in the playground and it is giving me this error. "Warning: curl_setopt() expects parameter 1 to be resource, null given in - on line 8" which is at least something positive to give some direction. 

 

https://www.tehplayground.com/myslcPbSqzzI3KvX

 

-Thanks!

I sorted out the error but now I don't get anything. 

 

This....

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

Should be

curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);

The $cURL was all lower case which was throwing the error. So that part is resolved but still not getting any output. 

Ok I am getting closer. I have the response as objects now but, I am not getting the true false values. I think that is the last thing I need to sort out. 

 

http://stream.dfwstormforce.com/inc/chasers/test.php

     $headers[] = 'Connection: Keep-Alive';
     $headers[] = 'Content-Type: application/json;charset=utf-8';
     $headers[] = 'Accept: application/json';

     $userAgent = 'php';

     $url = 'http://domain.com: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_HTTPHEADER, $headers);
     curl_setopt($cURL, CURLOPT_USERAGENT, $useragent);
     curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($cURL, CURLOPT_HTTPGET, true);
     
     $result = curl_exec($cURL);
     
     curl_close($cURL);
    
// Lets get and parse the data and create the variables

     $obj = json_decode($result); 
     echo('<pre>'.print_r($obj,1).'</pre>');
     echo $obj->name;
     echo $obj->isConnected;

-Thanks

Ok I am getting closer. I have the response as objects now but, I am not getting the true false values. I think that is the last thing I need to sort out.

 

 

false is displayed as an empty string.

echo $obj->isConnected?'true':'false;

I understand but I don't follow what you're referring to in relation to my current issue at the moment with the true/false values not being displayed. $cURL is the variable and I don't see any that are all lower case. I've already addressed and corrected that a couple of posts back.

 

false is displayed as an empty string.

echo $obj->isConnected?'true':'false;

 

 

Thanks! That works! I forgot all about that. Forgot when it returns values nothing is false and 1 is true and that had to be added. You left off the ' after false but I caught it. 

 

Just a question though for future reference. Why isn't true/false value returned when its parsed when its in the original string when you call it in a browser but not in the script?

 

-Thanks 

  • Solution

Here is the final result. Thanks to everyone who contributed their time and skills in helping me with this in my learning process. Now on to trying to take these variables to update the DB. If there is no further objections or suggestions here is the final code. 

$headers[] = 'Connection: Keep-Alive';
     $headers[] = 'Content-Type: application/json;charset=utf-8';
     $headers[] = 'Accept: application/json';

     $userAgent = 'php';

     $url = 'http://domain.com: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_HTTPHEADER, $headers);
     curl_setopt($cURL, CURLOPT_USERAGENT, $useragent);
     curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($cURL, CURLOPT_HTTPGET, true);
     
     $result = curl_exec($cURL);
     
     curl_close($cURL);
    
// Lets get and parse the data and create the variables

     $obj = json_decode($result);
     $chaser = $obj->name;
     $streamStatus = $obj->isConnected?'true':'false';
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.