cgnetwork/simple-video-api
Lightweight Laravel/PHP wrapper for a simple video API. Provides straightforward methods to upload, fetch, and manage videos, with clean request handling and easy integration into your app or services. Ideal for quick prototypes and small backends.
Installation
composer require cgnetwork/simple-video-api
Publish the config file (if needed):
php artisan vendor:publish --provider="Cgnetwork\SimpleVideoApi\SimpleVideoApiServiceProvider" --tag="config"
Configuration
Edit config/simple-video-api.php with your API credentials (e.g., YouTube, Vimeo, or self-hosted endpoints).
Example for YouTube:
'default' => 'youtube',
'youtube' => [
'api_key' => env('YOUTUBE_API_KEY'),
'base_url' => 'https://www.googleapis.com/youtube/v3/',
],
First Use Case: Fetch a Video
use Cgnetwork\SimpleVideoApi\Facades\SimpleVideoApi;
$video = SimpleVideoApi::get('youtube')->video('dQw4w9WgXcQ');
dd($video->title, $video->description);
Fetching Video Metadata
// Get a single video
$video = SimpleVideoApi::get('vimeo')->video('123456789');
// Get video stats (views, likes, etc.)
$stats = $video->stats;
Searching Videos
$results = SimpleVideoApi::get('youtube')->search('laravel tutorials', 5);
foreach ($results as $video) {
echo $video->title . "\n";
}
Uploading Videos (Self-Hosted)
$response = SimpleVideoApi::get('selfhosted')->upload(
file_path: 'path/to/video.mp4',
title: 'My Video',
description: 'A test upload'
);
Integration with Eloquent
// Store video ID in a database model
$post->video_id = 'dQw4w9WgXcQ';
$post->save();
// Retrieve metadata later
$video = SimpleVideoApi::get('youtube')->video($post->video_id);
$video = Cache::remember("video_{$videoId}", now()->addHours(1), function () use ($videoId) {
return SimpleVideoApi::get('youtube')->video($videoId);
});
try {
$video = SimpleVideoApi::get('youtube')->video('invalid_id');
} catch (\Cgnetwork\SimpleVideoApi\Exceptions\ApiException $e) {
Log::error("Video API error: " . $e->getMessage());
return response()->json(['error' => 'Video not found'], 404);
}
// Example: Track API calls in a middleware
public function handle($request, Closure $next) {
$response = $next($request);
if ($request->route()->getName() === 'video.api') {
$this->trackApiUsage('youtube');
}
return $response;
}
API Key Leaks
.env:
YOUTUBE_API_KEY=your_key_here
Rate Limits
$quota = SimpleVideoApi::get('youtube')->quota();
Log::info("Remaining quota: " . $quota->remaining);
use Symfony\Component\Process\Exception\TimeoutException;
try {
$video = SimpleVideoApi::get('youtube')->video($id);
} catch (TimeoutException $e) {
sleep(2); // Wait before retry
retry();
}
Deprecated Endpoints
/videos and /search before production use.Self-Hosted Quirks
POST /upload with multipart/form-data).debug to true in config/simple-video-api.php to log raw API responses:
'debug' => env('APP_DEBUG', false),
handleResponse method in a service provider:
SimpleVideoApi::extend('youtube', function ($app) {
return new \Cgnetwork\SimpleVideoApi\Adapters\YouTubeAdapter([
'api_key' => config('services.youtube.key'),
'debug' => true,
]);
});
public function test_video_fetch()
{
$mockResponse = ['id' => 'test123', 'title' => 'Mock Video'];
$this->mock(\Cgnetwork\SimpleVideoApi\Adapters\YouTubeAdapter::class, function ($mock) {
$mock->shouldReceive('video')->andReturn($mockResponse);
});
$video = SimpleVideoApi::get('youtube')->video('test123');
$this->assertEquals('Mock Video', $video->title);
}
Add New Adapters Extend the package by creating a custom adapter:
namespace App\Providers;
use Cgnetwork\SimpleVideoApi\Adapters\AdapterInterface;
use Cgnetwork\SimpleVideoApi\Adapters\AbstractAdapter;
class CustomAdapter extends AbstractAdapter implements AdapterInterface
{
public function video($id)
{
// Custom logic for your video API
return $this->request("GET", "/videos/{$id}");
}
}
Register it in AppServiceProvider:
SimpleVideoApi::extend('custom', function () {
return new CustomAdapter(config('services.custom'));
});
Override Default Behavior
Bind the facade to a custom manager in AppServiceProvider:
$this->app->bind(\Cgnetwork\SimpleVideoApi\Contracts\Manager::class, function ($app) {
return new \App\Services\CustomVideoManager();
});
Customize Response Handling
Modify how responses are parsed by overriding the parseResponse method in your adapter:
protected function parseResponse($response, $method, $endpoint)
{
// Custom parsing logic
return json_decode($response, true);
}
How can I help you explore Laravel packages today?