Sapling Logo
Tone API quickstart

PHP Tone Checker

Analyze overall and sentence-level tone from a PHP application across 28 fine-grained categories.

  • HTTPS POST
  • JSON HTTP API
  • Bearer API key
POST PHP logo PHP /api/v1/tone

PHP tone checker quickstart

Send text to the tone endpoint. The response ranks overall tones and returns a separate probability breakdown for every sentence.

No language-specific SDK is required. The example uses a standard or commonly used HTTP client to call Sapling's JSON API.
PHP /api/v1/tone

This example uses PHP's cURL extension.

<?php

$apiKey = getenv('SAPLING_API_KEY');
if (!$apiKey) {
    throw new RuntimeException('Set SAPLING_API_KEY first.');
}

$payload = json_encode([
    'text' => 'Really stoked about this! Will it be ready by next week?',
], JSON_THROW_ON_ERROR);

$request = curl_init('https://api.sapling.ai/api/v1/tone');
curl_setopt_array($request, [
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $payload,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . $apiKey,
        'Content-Type: application/json',
    ],
    CURLOPT_RETURNTRANSFER => true,
]);

$body = curl_exec($request);
if ($body === false) {
    throw new RuntimeException(curl_error($request));
}

$status = curl_getinfo($request, CURLINFO_RESPONSE_CODE);
curl_close($request);

if ($status < 200 || $status >= 300) {
    throw new RuntimeException("Sapling API error {$status}: {$body}");
}

echo $body;
Example response application/json
{
  "overall": [
    [0.9247, "curious", "🤓"],
    [0.0455, "excited", "😀"],
    [0.0057, "confused", "😕"]
  ],
  "results": [
    [[0.9937, "excited", "😀"]],
    [[0.5469, "confused", "😕"], [0.3993, "curious", "🤓"]]
  ],
  "sents": [
    "Really stoked about this!",
    "Will it be ready by next week?"
  ]
}

Probabilities are returned in descending order as probability, tone, and emoji tuples. Use sentence results when a message contains mixed tones.

About PHP

PHP is a popular scripting language for the web. It is typically processed on a web server as a module, daemon or CGI executable. Many popular web services are written in PHP including well-known websites such as Facebook and frameworks like Wordpress.