浏览器无法加载 PHP 脚本,但终端可以?

Browser can't load PHP script, but terminal can?

我正在创建一个智能家居助手,我尝试使用基本的 Chrome TTS 和其他 api,然后落在了 Google Cloud Platform Text To Speech WaveNet 东西上。我使用 PHP 代码示例将音频放入文件夹(名为剪辑)中的文件中。

当我尝试在浏览器中 运行 PHP 文件时,它不起作用,但是 运行 使用 php 命令在 [=38] =] OS 终端可以正常工作并成功创建文件,没有错误。

我试过使用 Node.js,但它没有用,因为我需要 运行 来自 HTML 页面的文件,而且我不想公开我的 Google 云平台 API 凭据。

<?php

header("Content-Type: application/json");

if(!isset($_GET['text'])) {
  json_encode(array(
    "success" => "false",
    "error" => "missingPhrase"
  ));
}

require 'vendor/autoload.php';

use Google\Cloud\TextToSpeech\V1\AudioConfig;
use Google\Cloud\TextToSpeech\V1\AudioEncoding;
use Google\Cloud\TextToSpeech\V1\SynthesisInput;
use Google\Cloud\TextToSpeech\V1\TextToSpeechClient;
use Google\Cloud\TextToSpeech\V1\VoiceSelectionParams;

$textToSpeechClient = new TextToSpeechClient();

$input = new SynthesisInput();
$input->setText($_GET['text']);
$voice = new VoiceSelectionParams();
$voice->setLanguageCode('en-US-Wavenet-D');
$audioConfig = new AudioConfig();
$audioConfig->setAudioEncoding(AudioEncoding::LINEAR16);

$number = 0;
$fi = new FilesystemIterator("clips", FilesystemIterator::SKIP_DOTS);
foreach ($fi as $f) {
  $number = $number + 1;
}
$number = $number + 1;

$resp = $textToSpeechClient->synthesizeSpeech($input, $voice, $audioConfig);
file_put_contents("clips/" . $number . '.mp3', $resp->getAudioContent());

echo json_encode(array(
  "file_name" => 'clips/' . $number . ".mp3"
));

?>

上述代码的结果导致默认 chrome "This page isn’t working" 无意义。

PS,我在 Stack Overflow 上查看了一些与我遇到的问题相关的其他答案,他们 NOT 解决了我的问题,但没有涵盖几乎相同的问题。

谢谢, 内森

在@jdp的帮助下,我得以解决。我需要 link 到包含授权凭据的 json 文件。现在可以了。 :)

$textToSpeechClient = new TextToSpeechClient(['credentials' => 'credentials.json']);