Skip to main content

PHP API example

Resources

Resources

Using PHP with MistServer

The first step is to set up a way to send HTTP POST requests to MistServer, with a JSON payload. An example of this is done in mistserver.php. CURL is used to handle the HTTP request. MistServer will respond with a JSON encoded reply, which is translated to an associative array.

If MistServer is hosted on the same machine that will be running the php, authorization isn't required. Otherwise, MistServer will reply telling us to authenticate using a special string. If this is the case, the password will be encrypted together with the authentication string, and sent back to MistServer along with the username. Once MistServer reports that the status is OK, the authentication part is stripped from the reply, and returned.

By default, minimal mode will be used. This slightly lowers the bandwidth, but more importantly the response will be less bulky and thus more readable.

Examples

In mistserver.php, showing errors has been purposely left out, so that it can be used as a library for your project. In index.php, a few simple usage examples can be found.

MistServer itself never replies with an error key directly in its main object, so when the library encounters cURL or API errors, it reports them there.

In index.php, a function getData() adds error printing around the main communication function. It returns false if there was an error and the data array if there wasn't.

Requesting configuration

getCurrentProtocols() calls getData() and asks MistServer to respond through the config call. We check if the communication was successful, and if the config key exists. Then, we loop over the configured protocols and print the connector name.

Sent

Array(
"config" => true
)

Received

Array(
"config" => Array(
"protocols" => Array(
0 => Array(
"connector" => "HTTP",
"online" => 1
),
[..]
),
[..]
)
)

Requesting logs & checking for errors

Using the example to read the logs and detect if there are any errors in it, print the most recent one. Note that MistServer only provides the last 100 log entries through the API, so there might not be any.

getLastErrorLog() also calls getData(), but this time requests the log array. If we get it back, we reverse the array to get the newest entries first, and then start looping over them, until we find an error. If we do, we print it.

Sent

Array(
"log" => true
)

Received

Array(
"log" => Array(
0 => 1494493673,
1 => "CONF",
2 => "Controller started"
),
[..]
)

Adding streams

For this we'll use the addstream call.

Two parameters need to be included: the stream name and source. There can be more options, depending on what kind of source it is. Note that if addstream is used, and a stream with that name already exists, it will be overwritten.

The addStream($name,$options) function calls getData() with these values, checks if the stream exists in the reply, and returns it.

Sent

Array(
"addstream" => Array(
"example" => Array(
"source" => "/path/to/file.flv"
)
)
)

Received

Array(
"streams" => Array(
"example" => Array(
"name" => "example",
"source" => "/path/to/file.flv"
),
"incomplete list" => 1
)
)

Removing Streams

To remove streams you will need to use the deletestream call.

The deleteStream($name) function calls getData(), and checks if the stream has indeed been removed.

Sent

Array(
"deletestream" => Array(
0 => "example"
)
)

Received

Array(
"streams" => Array(
"incomplete list" => 1
)
)

Building your own

Of course you can use the examples above to just implement the communication to MistServer in your project, but should you wish to build your own we recommend reading up on the API. We've tried to make it as clear & easy to use possible, but should you run into questions feel free to contact us.