peranetwork / viewstats-api
A PHP wrapper for the ViewStats API
Requires
- php: >=7.4
- ext-curl: *
- ext-json: *
- ext-openssl: *
This package is not auto-updated.
Last update: 2025-05-27 15:58:21 UTC
README
This PHP wrapper provides an easy-to-use interface for interacting with the ViewStats API. It handles authentication, request sending, and response processing, including decryption of encrypted responses.
ViewStats is a platform for tracking and analyzing YouTube channel statistics. The API provides access to various data points, including channel information, video statistics, and more.
Address: ViewStats
Features
- Simple interface for making GET and POST requests to the ViewStats API
- Automatic handling of authentication tokens
- Built-in error handling and logging
- Decryption of encrypted API responses
- Easy integration into existing PHP projects
Requirements
- PHP 7.4 or higher
- cURL extension
- OpenSSL extension
Installation
You can install this package via Composer:
composer require peranetwork/viewstats-api
Usage
Basic usage
require 'vendor/autoload.php'; use ViewStatsWrapper\ViewStatsAPI; $apiToken = '32ev9m0qggn227ng1rgpbv5j8qllas8uleujji3499g9had6oj7f0ltnvrgi00cq'; $api = new ViewStatsAPI($apiToken); try { $popularChannels = $api->get('/channels/popular', ['total' => 1]); print_r($popularChannels); } catch (\ViewStatsWrapper\Exceptions\APIException $e) { echo "API Error: " . $e->getMessage(); } catch (\ViewStatsWrapper\Exceptions\DecryptionException $e) { echo "Decryption Error: " . $e->getMessage(); }
Making GET requests
$params = ['param1' => 'value1', 'param2' => 'value2']; $response = $api->get('/endpoint', $params);
Making POST requests
$data = ['key1' => 'value1', 'key2' => 'value2']; $response = $api->post('/endpoint', $data);
API Endpoints
Here are some example endpoints you can use with this wrapper:
GET Requests
-
Channel Information:
$api->get('/channels/@delba');
-
Channel About Me:
$api->get('/channels/@delba/aboutMe');
-
Channel Stats:
$api->get('/channels/@delba/stats', [ 'range' => '28', 'groupBy' => 'daily', 'sortOrder' => 'ASC', 'withRevenue' => 'true', 'withEvents' => 'true', 'withBreakdown' => 'false', 'withToday' => 'false' ]);
-
All-time Channel Stats:
$api->get('/channels/@delba/stats', [ 'range' => 'alltime', 'groupBy' => 'daily', 'sortOrder' => 'ASC', 'withRevenue' => 'true', 'withEvents' => 'false', 'withBreakdown' => 'false' ]);
-
Monthly Channel Stats:
$api->get('/channels/@delba/stats', [ 'range' => '365', 'groupBy' => 'monthly', 'sortOrder' => 'ASC', 'withRevenue' => 'true', 'withEvents' => 'true', 'withBreakdown' => 'false', 'withToday' => 'false' ]);
-
Long and Short Videos:
$api->get('/channels/@delba/longsAndShorts');
-
Featured Video:
$api->get('/channels/@delba/featuredVideo');
-
Channel Averages:
$api->get('/channels/@delba/averages');
-
Channel Videos:
$api->get('/channels/@delba/videos', [ 'orderBy' => 'uploadDate', 'limit' => '40', 'offset' => '0', 'withThumbnail' => 'true', 'type' => 'lf' ]);
-
Channel Projections:
$api->get('/channels/@delba/projections/milestones');
-
Similar Channels:
$api->get('/channels/@delba/similar');
-
Popular Channels:
$api->get('/channels/popular', ['total' => '6']);
POST Requests
-
Channel Rankings:
$api->post('/rankings/channels', [ 'interval' => 'weekly', 'sortBy' => 'subs', 'made_for_kids' => true, 'show_music' => true, 'category_id' => null, 'country' => null, 'show_movies' => true ]);
-
Video Rankings:
$api->post('/rankings/videos', [ 'interval' => 7, 'includeKids' => true, 'includeMusic' => true, 'country' => 'all', 'categoryId' => 9999, 'videoFormat' => 'all' ]);
Error Handling
The wrapper throws two types of exceptions:
APIException
: For general API errors, including HTTP errors.DecryptionException
: Specifically for errors during the decryption of API responses.
It's recommended to catch these exceptions separately to handle different types of errors appropriately.
Logging
The wrapper includes a basic logging mechanism. Logs are written to debug.log
in the project root. You can customize the logging behavior by modifying the Logger
class.
Security
- API tokens are sent securely in the request headers.
- Encrypted responses are automatically decrypted using AES-256-GCM.
- Sensitive data like API tokens should be stored securely and not hard-coded in your application.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
Support
If you encounter any problems or have any questions, please open an issue on the GitHub repository.