PHP code example of solanaguide / laravel-x402

1. Go to this page and download the library: Download solanaguide/laravel-x402 library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

solanaguide / laravel-x402 example snippets


return [
    // ...
    SolanaGuide\X402\X402Bundle::class => ['all' => true],
];

// Routes that cost $0.001 USDC per request
Route::get('/api/weather', WeatherController::class)
    ->middleware('x402:0.001');

// With a description
Route::get('/api/premium', PremiumController::class)
    ->middleware('x402:0.01,Premium weather data');

// config/x402.php
'routes' => [
    'GET /api/premium/*' => ['price' => '0.01', 'description' => 'Premium content'],
    'POST /api/generate' => ['price' => '0.05', 'description' => 'AI generation'],
],

use Symfony\Component\Routing\Annotation\Route;

#[Route('/api/weather')]
public function weather(): Response
{
    // Set _x402_price on the request attributes
    return $this->json(['temp' => 72]);
}

// app/Http/Kernel.php — apply to all web routes
protected $middlewareGroups = [
    'web' => [
        // ... existing middleware
        \SolanaGuide\X402\Middleware\PaymentRequiredMiddleware::class,
    ],
];

$detector = app(AiCrawlerDetector::class);
$detector->addPattern('MyCustomBot');
bash
php artisan vendor:publish --tag=x402-config
bash
php artisan x402:check