Search code examples
phpwowza

Issues creating, updating and retrieving data from Wowza Streaming Engine using PHP REST API?


I am getting started with the Wowza PHP Library and I am having trouble connecting to the streaming engine i have installed locally. As directed from https://github.com/WowzaMediaSystems/wse-rest-library-php, I installed composer, created the config file with my server host and authentication settings

wowza_config.php

define("WOWZA_HOST","http://localhost:8087/v2");
define("WOWZA_SERVER_INSTANCE", "_defaultServer_");
define("WOWZA_VHOST_INSTANCE", "_defaultVHost_");
define("WOWZA_USERNAME", "admin");
define("WOWZA_PASSWORD", "admin");

Then when trying tests to retrieve data from the server for example:

index.php

<?php


require_once ('vendor/autoload.php');
require_once("config/wowza_config.php");


// It is simple to create a setup object for transporting our settings
$setup = new Com\Wowza\Entities\Application\Helpers\Settings();
$setup->setHost(WOWZA_HOST);
$setup->setUsername(WOWZA_USERNAME);
$setup->setPassword(WOWZA_PASSWORD);

$sf = new Com\Wowza\Statistics($setup);
// get stats per application
$wowzaApplication = new Com\Wowza\Application($setup, 'vod');
// get total server stats
$server = new Com\Wowza\Server($setup, 'http://localhost:8087/v2');
$response = $sf->getServerStatistics($server);
// get stats historical for given application
// $response = $sf->getApplicationStatisticsHistory($wowzaApplication);
// $response = $sf->getApplicationStatistics($wowzaApplication);
// get incoming stream stats for given application


var_dump($response);
?>

I get the error

object(stdClass)#8 (4) { ["message"]=> string(40) "The request requires user authentication" ["code"]=> string(3) "401" ["wowzaServer"]=> string(5) "4.7.7" ["success"]=> bool(false) } 

I have triple checked to confirm the credentials I am using match those of the servers but cant figure out what I am doing wrong


Solution

  • One thing I notice: In your settings you don't set a value for "useDigest". So it defaults to false (see https://github.com/WowzaMediaSystems/wse-rest-library-php/blob/master/src/Entities/Application/Helpers/Settings.php). Then when you call getServerStatistics() eventually it calls "sendRequest()" in the Wowza class (see https://github.com/WowzaMediaSystems/wse-rest-library-php/blob/master/src/Wowza.php). And in this class, it only adds username and password to the request if "useDigest" is set to true in the settings:

    if ($this->settings->isUseDigest()) {
       curl_setopt($ch, CURLOPT_USERPWD, $this->settings->getUsername() . ':' . $this->settings->getPassword());
       curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
    }
    

    So I think it's not attaching the username and password to your request at all.

    Therefore I suggest you add

    $setup->setUseDigest(true);
    

    to your code when you're configuring the Settings object, and that should help.