blitzr/php-client
Official PHP client for the Blitzr API. Install via Composer, authenticate with your API key, and access Blitzr resources like artists through a simple, lightweight client (e.g., getArtist). Includes docs and links to the API reference.
composer require blitzr/php-client
use Blitzr\BlitzrClient;
$blitzr = new BlitzrClient(config('services.blitzr.api_key'));
config/services.php for security:
'blitzr' => [
'api_key' => env('BLITZR_API_KEY'),
],
$artist = $blitzr->getArtist('year-of-no-light');
name, tracks, albums).if ($artist) {
dd($artist['name']); // Output: "Year of No Light"
}
AppServiceProvider:
public function register()
{
$this->app->singleton(BlitzrClient::class, function ($app) {
return new BlitzrClient(config('services.blitzr.api_key'));
});
}
use Blitzr\BlitzrClient;
public function showArtist(BlitzrClient $blitzr, $artistName)
{
$artist = $blitzr->getArtist($artistName);
return view('artist', compact('artist'));
}
Fetching Tracks/Albums:
$tracks = $blitzr->getArtistTracks('year-of-no-light');
$albums = $blitzr->getArtistAlbums('year-of-no-light');
getArtistTracks($name, $page = 1) to handle paginated results.Search Functionality:
$results = $blitzr->search('darkwave');
results['artists'], results['tracks']).Error Handling:
try-catch block:
try {
$artist = $blitzr->getArtist('invalid-name');
} catch (\Blitzr\Exceptions\ApiException $e) {
Log::error($e->getMessage());
return response()->json(['error' => 'Artist not found'], 404);
}
API Resources: Transform Blitzr responses into Laravel API Resources:
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class ArtistResource extends JsonResource
{
public function toArray($request)
{
return [
'name' => $this->name,
'tracks_count' => count($this->tracks),
];
}
}
return new ArtistResource($blitzr->getArtist('year-of-no-light'));
Caching Responses: Use Laravel’s cache to avoid redundant API calls:
$artist = Cache::remember("blitzr_artist_{$artistName}", now()->addHours(1), function () use ($blitzr, $artistName) {
return $blitzr->getArtist($artistName);
});
Queueing Long-Running Tasks: Dispatch a job for heavy operations (e.g., fetching all tracks for an artist):
FetchArtistTracksJob::dispatch($blitzr, $artistName);
API Key Exposure:
.env and config/services.php..env file is in your .gitignore.Rate Limiting:
use Symfony\Component\Process\Exception\TimeoutException;
try {
$artist = $blitzr->getArtist($name);
} catch (TimeoutException $e) {
sleep(2); // Wait before retrying
retry();
}
Deprecated Methods:
Response Format:
stdClass vs. associative arrays). Normalize them:
$artist = json_decode(json_encode($blitzr->getArtist($name)), true);
Enable Debug Mode:
$blitzr = new BlitzrClient('api_key', ['debug' => true]);
storage/logs/blitzr.log.Validate API Responses:
Use Laravel’s Validator to check required fields:
$validator = Validator::make($artist, [
'name' => 'required|string',
'tracks' => 'array',
]);
Custom Endpoints:
class ExtendedBlitzrClient extends BlitzrClient
{
public function getCustomData($endpoint)
{
return $this->request('GET', "/api/{$endpoint}");
}
}
Middleware:
$blitzr->getMiddleware()->push(function ($request) {
$request->headers->set('X-Custom-Header', 'value');
});
Testing:
$mock = Mockery::mock(BlitzrClient::class);
$mock->shouldReceive('getArtist')
->once()
->andReturn(['name' => 'Mock Artist']);
Http facade to stub HTTP calls:
Http::fake([
'api.blitzr.io/*' => Http::response(['name' => 'Mock Artist']),
]);
Base URL:
$blitzr = new BlitzrClient('api_key', ['base_url' => 'https://custom.blitzr.io']);
Timeouts:
$blitzr = new BlitzrClient('api_key', ['timeout' => 30]);
Batch Processing:
collect() to process large datasets efficiently:
collect($blitzr->getArtistTracks('year-of-no-light'))
->each(function ($track) {
// Process each track
});
Webhooks:
Route::post('/blitzr-webhook', function (Request $request) {
$payload = $request->json()->all();
// Process webhook data
});
Documentation Sync:
How can I help you explore Laravel packages today?