HTTP API SQLite Examples


#1

This demonstration will use HttpGuzzle, a PHP library to illustrate how to retrieve data from the API. Note that these instructions are performed on a Linux server. The installation procedures should be relatively the same as long as your path is configured c correctly.

Installing HttpGuzzle
First, install PHP Composer which is required for HttpGuzzle. This should be installed in the directory in your web hosting directory that you wish to run the application from.
This requires PHP 5.5.0 or later. Also, ensure that allow_url_fopen is enabled in the system php.ini. For more information please visit Overview — Guzzle Documentation

illusion% curl -sS https://getcomposer.org/installer | php
All settings correct for using Composer
Downloading...

Composer successfully installed to: /var/www/html/bot/composer.phar
Use it: php composer.phar

illusion% php composer.phar require guzzlehttp/guzzle:~6.0
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)

- Installing guzzlehttp/promises (1.1.0)
Downloading: 100%         

- Installing psr/http-message (1.0)
Downloading: 100%         

- Installing guzzlehttp/psr7 (1.2.3)
Downloading: 100%         

- Installing guzzlehttp/guzzle (6.1.1)
Downloading: 100%         

Writing lock file
Generating autoload files

Now, HttpGuzzle is installed and ready to use. Let us start by making a file that will request and present the keys from a database table:

getKeys.php

require 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;

try
{
    $client = new GuzzleHttp\Client(['headers' => [ 'webauth' => 'YOUR_WEBAUTH_KEY' ] ]);
	$response = $client->get('http://localhost:25000/dbquery?table=ytSettings&getKeys');

	$result = $response->getBody();
	$object = json_decode($result);
	$table_name = $object->table->table_name;
	echo "Found Table: ".$table_name."<br>";
	echo "Found Keys:<br>";
	foreach($object->table->keylist as $keys)
	{
        echo $keys->key."<br>";
    }
    
} 
catch (ClientException $e)
{
    $response = $e->getResponse();
    $result = $response->getBody();
    $object = json_decode($result);
    echo "Exception has occurred: ". $object->{'error'};
}

This will return:

Found Table: ytSettings
Found Keys:
activePlaylistname
volume
randomizePlaylist
announceInChat
songRequestsEnabled
songRequestsMaxParallel
playlistDJname

Now, let us pull back data related to a key:

getData.php

require 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;

try
{   
	$client = new GuzzleHttp\Client(['headers' => [ 'webauth' => ‘YOUR_WEBAUTH_KEY’ ] ]);
	$response = $client->get('http://localhost:25000/dbquery?table=ytSettings&getData=volume');

	$result = $response->getBody();
	$object = json_decode($result);
	$table_name = $object->table->table_name;
	$key = $object->table->key;
	$value = $object->table->value;
	echo "Found Table: ".$table_name."<br>";
	echo "Found Key: ".$key."<br>";
	echo "Found Value: ".$value."<br>";
} 
catch (ClientException $e)
{
    $response = $e->getResponse();
    $result = $response->getBody();
    $object = json_decode($result);
    echo "Exception has occurred: ". $object->{'error'};
}

This will return:

Found Table: ytSettings
Found Key: getData
Found Value: 19

Option to toggle oauth for websocket possible?