首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >浏览器无法加载PHP脚本,但终端可以吗?

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

Stack Overflow用户
提问于 2019-04-09 23:51:09
回答 1查看 0关注 0票数 0

我正在创建一个智能家庭助理,我尝试使用基本的Chrome TTS,其他api,然后落在Google云平台文本到语音WaveNet的东西上。我使用PHP代码示例将音频放入文件夹(命名剪辑)中的文件中。

当我尝试在浏览器中运行PHP文件时,它不起作用,但在Mac OS终端中使用php命令运行它确实有效,并且没有错误地成功创建文件。

我尝试过使用Node.js,但它不起作用,因为我需要从HTML页面运行该文件而我不想公开我的Google Cloud Platform API凭据。

代码语言:javascript
复制
<?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“此页面无法正常工作”非定义。

PS,我看着堆栈溢出一些其他的答案与我有问题,他们解决我的问题,并没有涵盖几乎同样的问题。

EN

回答 1

Stack Overflow用户

发布于 2019-04-10 09:13:28

需要链接到包含授权凭据的json文件。

代码语言:javascript
复制
$textToSpeechClient = new TextToSpeechClient(['credentials' => 'credentials.json']);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100009003

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档