Connect Your PHP Backend to Agent & LLM Analytics
Overview
Use the REST API to connect your website from a PHP backend. Please contact us if you need help.
Step 1: Copy Your Access Token
- Navigate to the Projects page
- Select your project
- Click Settings
- Copy your access token
Step 2: Start Tracking Visits
Paste in this function that makes an HTTP request to the REST API with detail about an incoming pageview request.
function track_visit_in_dark_visitors() {
$curl = curl_init('https://api.darkvisitors.com/visits');
curl_setopt_array($curl, [
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $YOUR_ACCESS_TOKEN,
'Content-Type: application/json',
],
CURLOPT_POSTFIELDS => json_encode([
'request_path' => $_SERVER['REQUEST_URI'],
'request_method' => $_SERVER['REQUEST_METHOD'],
'request_headers' => getallheaders(),
], JSON_UNESCAPED_SLASHES),
CURLOPT_NOSIGNAL => true,
CURLOPT_TIMEOUT_MS => 50,
]);
@curl_exec($curl);
curl_close($curl);
}
In the endpoints where you serve your pages, call the function for each incoming pageview request. If you can, add this in middleware to track incoming requests to all pages from a single place.
track_visit_in_dark_visitors();
Tips
- If you change this code, keep it non-blocking to avoid adding latency to your page response.
- If you change this code, keep handling errors in a way that doesn't make your page response fail too.
- Strip out any sensitive HTTP headers you don't want to send.
Step 3: Test Your Integration
- Navigate to the Projects page
- Select your project
- Click Settings
- Click Send a Test Visit
- Click Realtime
If your website is correctly connected, you should see visits from the Dark Visitor agent in the realtime timeline within a few seconds.