How to Use the PHP Text to Speech API

Convert text to voice directly from PHP using the Narakeet REST API. No special PHP extension or text to speech package required — the built-in cURL functions handle everything. This page walks through a working example and explains how to tweak the output for your project.

Need background on API keys, endpoints, or audio formats first? Start with the main Text to Speech API reference.

How to Convert Text to Speech Using PHP

To convert text to speech using PHP, POST your text to the Narakeet API endpoint with cURL. Provide your API key in the x-api-key header and the text in the request body. The response is a binary audio stream that you write straight to a file.

<?php

$apikey = getenv('NARAKEET_API_KEY');
$text = 'Hi there from PHP';
$voice = 'brian';

$url = "https://api.narakeet.com/text-to-speech/mp3?voice=$voice";

$options = [
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $text,
    CURLOPT_HTTPHEADER => [
        'Accept: application/octet-stream',
        'Content-Type: text/plain',
        "x-api-key: $apikey",
    ],
    CURLOPT_FILE => fopen('output.mp3', 'w'),
];

$curl = curl_init();
curl_setopt_array($curl, $options);
curl_exec($curl);
curl_close($curl);

?>

Before running the script, set NARAKEET_API_KEY in your environment. You can get a key from the Managing API Keys page.

PHP Text to Speech API

Append query parameters to the endpoint URL to control voice selection and audio characteristics with the PHP text to speech API.

Parameter Example Value Effect
voice brian Selects the speaking voice (full list)
voice-speed 1.2 Speeds up reading (values below 1 slow it down)
voice-volume soft Sets output volume (soft, medium, loud)

Switch between /mp3, /m4a, and /wav endpoints to change the audio format. WAV is only available through the polling API.

You can also set voice pitch, add pauses, and use multi-voice scripts through the script header format. See Configuring Audio Tasks for details.

PHP Text to Speech

Narakeet offers a practical PHP text to speech integration that slots into any PHP application — web frontend, CLI tool, or queued worker. Your PHP code makes one HTTP request, and the API returns finished audio. The PHP text to speech API handles 900 voices across 100 languages, covering MP3, M4A, and WAV formats.

For the complete source code, see the PHP streaming API example and the PHP polling API example on GitHub.