Documentation / Integrations · PHP

PHP: a small controller, a clear boundary.

Laravel’s HTTP client and the framework request object make the relay small. The important part is the boundary: the secret stays in the environment, and the browser only sees the public key.

Last updated

Laravel relay

app/Http/Controllers/HumanCheckController.phpLaravel
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class HumanCheckController extends Controller
{
    public function __invoke(Request $request)
    {
        $evidence = $request->validate([
            'page' => 'nullable|string|max:512',
            'clientSignals' => 'array',
            'events' => 'array',
        ]);

        $session = Http::withToken(config('services.volance.public_key'))
            ->timeout(1)
            ->get('https://app.volance.com/api/trace/session')
            ->throw()
            ->json();

        $result = Http::withToken(config('services.volance.secret_key'))
            ->timeout(1)
            ->post('https://app.volance.com/api/trace/score', [
                'publicKey' => config('services.volance.public_key'),
                'sid' => $session['sessionId'],
                'manifest' => $session['manifest'],
                'page' => $evidence['page'] ?? '/',
                'clientSignals' => $evidence['clientSignals'] ?? [],
                'events' => $evidence['events'] ?? [],
                'requestSignals' => [
                    'visitorIp' => $request->ip(),
                    'cookiePresent' => $request->hasCookie('_session'),
                    'visitorUserAgent' => $request->userAgent(),
                    'acceptLanguage' => $request->header('accept-language'),
                ],
            ])
            ->json();

        return response()->json([
            'action' => $result['action'],
            'score' => $result['score'],
            'requestId' => $result['requestId'] ?? null,
        ]);
    }
}

Config

config/services.php + .envServer-only secrets
// config/services.php
'volance' => [
    'public_key' => env('VOLANCE_PUBLIC_KEY'),
    'secret_key' => env('VOLANCE_SECRET_KEY'),
],

// .env
VOLANCE_PUBLIC_KEY=pk_...
VOLANCE_SECRET_KEY=sk_...

What to watch

  • Config cache: run php artisan config:cache after changing the secret; a stale cache is the usual “why is it 401?” cause.
  • Trusted proxies: if you are behind a load balancer, configure trusted proxies so $request->ip() is the visitor, not the balancer.
  • Cookies: pass whether a first-party cookie came back, not its value.
  • Timeouts: keep the HTTP client timeout under a second and decide the fallback per route.
  • Caching: do not cache the relay response; cache the manifest for its one-hour lifetime if you need to.

WordPress

For WordPress, use the maintained plugin or the PHP relay in the WordPress guide. The boundary is the same: sk_ in wp-config.php or the environment, never in a shortcode.

Related

Free to start. 10,000 scores a month, one project, no credit card.

Get started free