PHP Text to Voice

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.

Text-to-Speech PHP Script Converter

A text-to-speech PHP script converter fits naturally into existing web stacks. Because PHP already powers the backend of most WordPress, Laravel, and Drupal sites, you can add audio generation without introducing a new language or runtime. Typical scenarios where a PHP script converter helps:

  • Turning blog posts into audio automatically when a WordPress post is published
  • Generating IVR phone menu prompts from a database of messages
  • Feeding translated content through a cron job to produce multilingual audio files
  • Creating downloadable MP3 versions of help articles in a support portal

For documents longer than 1 KB, or when you need WAV output, switch to the Long Content (Polling) API. A complete PHP polling example is available at https://github.com/narakeet/text-to-speech-polling-api-php-example.

PHP Text to Speech API Options

Append query parameters to the endpoint URL to control voice selection and audio characteristics.

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.