使用curl从textreader.surge.sh下载音频文件

时间:2019-02-18 04:42:52

标签: curl

我正在尝试向以下网站发送卷曲请求:https://textreader.surge.sh/。我想发送文本,并让服务器为我提供该文本的音频文件,因为它是一个文本到语音程序。

有人知道怎么写吗?

1 个答案:

答案 0 :(得分:0)

首先对https://textreader.surge.sh/进行HTTP GET请求,然后对https://us-central1-sunlit-context-217400.cloudfunctions.net/streamlabs-tts进行HTTP OPTIONS请求,该请求的正文为空,并且HTTP标头为Access-Control-Request-Method: POSTAccess-Control-Request-Headers: content-type,并且 Origin: https://textreader.surge.sh并验证响应是否为okenter image description here),然后向标头https://us-central1-sunlit-context-217400.cloudfunctions.net/streamlabs-ttsReferer: https://textreader.surge.sh/发送给Content-Type: application/json;charset=utf-8的HTTP POST请求和Origin: https://textreader.surge.sh并让POST正文包含以JSON编码的数据"text":"<the text you wish spoken here>","voice":"Brian"},响应看起来像

{
    "success": true,
    "speak_url": "https://polly.streamlabs.com/v1/speech?OutputFormat=ogg_vorbis&Text=penis&VoiceId=Brian&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIHKNQTJ7BGLEFVZA/20190218/us-west-2/polly/aws4_request&X-Amz-Date=20190218T112454Z&X-Amz-SignedHeaders=host&X-Amz-Expires=900&X-Amz-Signature=07652663d5024414ae4b4087fa39f9eaa8b912cc04dc7a5d7abecdaed5585396"
}

现在只需向带有HTTP标头Referer: https://textreader.surge.sh/Origin: https://textreader.surge.sh的“扬声器URL”发出HTTP GET请求,瞧,响应就是您的音频,格式为og​​g / vorbis(基本上就像非专有的,免专利和免版税的MP3版本)

  • 令人惊讶的是,此网站完全不使用cookie。

使用PHP / hhb_curl看起来像

<?php
declare (strict_types = 1);
require_once('hhb_.inc.php');
$hc = new hhb_curl('', true);
$hc->exec('https://textreader.surge.sh');
$response = $hc->setopt_array(array(
    CURLOPT_CUSTOMREQUEST => 'OPTIONS',
    CURLOPT_HTTPHEADER => array(
        'Access-Control-Request-Method: POST',
        'Access-Control-Request-Headers: content-type',
    ),
    CURLOPT_URL => 'https://us-central1-sunlit-context-217400.cloudfunctions.net/streamlabs-tts',
))->exec()->getStdOut();
if (trim($response) !== "ok") {
    throw new \LogicException("unexpected response from https://us-central1-sunlit-context-217400.cloudfunctions.net/streamlabs-tts: {$response}");
}
$json = $hc->setopt_array(array(
    CURLOPT_URL => 'https://us-central1-sunlit-context-217400.cloudfunctions.net/streamlabs-tts',
    CURLOPT_CUSTOMREQUEST => null,
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => json_encode(array(
        'text' => 'the text you wish spoken here',
        'voice' => 'Brian'
    )),
    CURLOPT_HTTPHEADER => array(
        'Referer: https://textreader.surge.sh/',
        'Content-Type: application/json;charset=utf-8',
        'Origin: https://textreader.surge.sh'
    )
))->exec()->getStdOut();
//var_dump($json);
$data = json_decode($json, true);
$ogg_vorbis_location = $data['speak_url'];
$ogg_vorbis_binary = $hc->setopt(CURLOPT_HTTPGET, 1)->exec($ogg_vorbis_location)->getStdOut();
file_put_contents("voice.ogg",$ogg_vorbis_binary);
echo "audio saved to voice.ogg.\n";