Basic request with cURL
No dependencies required - PHP's built-in cURL extension handles the request:
<?php
$apiKey = 'cai_live_your_key_here';
$input = 'My invoice shows a double charge this month.';
$payload = json_encode([
'input' => $input,
'categories' => ['billing', 'technical_bug', 'account_access', 'general'],
]);
$ch = curl_init('https://api.classifaily.com/v1/classify');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json',
],
CURLOPT_TIMEOUT => 10,
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$result = json_decode($response, true);
echo $result['result']['label']; // "billing"
echo $result['result']['confidence']; // 0.97
Error handling
<?php
if (curl_errno($ch)) {
throw new RuntimeException('cURL error: ' . curl_error($ch));
}
if ($httpCode !== 200) {
$err = json_decode($response, true);
throw new RuntimeException('API error ' . $httpCode . ': ' . ($err['error'] ?? 'unknown'));
}
Reusable classify function
<?php
function classifaily_classify(string $input, array $categories, bool $explain = false): array {
$apiKey = getenv('CLASSIFAILY_KEY');
$payload = json_encode(array_filter([
'input' => $input,
'categories' => $categories,
'explain' => $explain ?: null,
]));
$ch = curl_init('https://api.classifaily.com/v1/classify');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json',
],
CURLOPT_TIMEOUT => 10,
]);
$body = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($code !== 200) {
throw new RuntimeException('classifaily error ' . $code);
}
return json_decode($body, true)['result'];
}
// Usage
$result = classifaily_classify(
'Double charge on my account',
['billing', 'technical_bug', 'account_access', 'general']
);
if ($result['confidence'] > 0.80) {
routeTicket($result['label']);
} else {
flagForHumanReview();
}
Batch classification
<?php
$items = [
['id' => 'q1', 'input' => 'Cannot log in after password reset.'],
['id' => 'q2', 'input' => 'Refund request for last month.'],
['id' => 'q3', 'input' => 'Please add dark mode.'],
];
$payload = json_encode([
'items' => $items,
'categories' => ['billing', 'technical_bug', 'account_access', 'feature_request'],
]);
$ch = curl_init('https://api.classifaily.com/v1/batch');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('CLASSIFAILY_KEY'),
'Content-Type: application/json',
],
]);
$results = json_decode(curl_exec($ch), true)['results'];
curl_close($ch);
foreach ($results as $r) {
echo $r['id'] . ': ' . $r['result']['label'] . PHP_EOL;
}
Using Guzzle
<?php
use GuzzleHttp\Client;
$client = new Client(['base_uri' => 'https://api.classifaily.com']);
$response = $client->post('/v1/classify', [
'headers' => [
'Authorization' => 'Bearer ' . getenv('CLASSIFAILY_KEY'),
'Content-Type' => 'application/json',
],
'json' => [
'input' => 'Cannot access my account.',
'categories' => ['billing', 'technical_bug', 'account_access', 'general'],
],
]);
$data = json_decode($response->getBody(), true);
echo $data['result']['label'];
Ready to integrate?
See the full API reference for all parameters and response schemas.
API Reference