我当前的PHP代码似乎不适用于IBM Watson Text To Speech。
以下代码返回以下输出:
{
"code_description": "Bad Request",
"code": 400,
"error": "No JSON object could be decoded"
}"
我一直在添加和删除几个cURL选项,以使其正常工作,但它仍返回上述错误。
<?php
$data="Hello World";
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_URL,"https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize");
curl_setopt($ch, CURLOPT_USERPWD, "$USERNAME:$password");
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
"Content-Type: application/json",
"Accept: audio/webm"
)
);
$server_output = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$info = curl_getinfo($ch);
curl_close ($ch);
我应该添加/删除哪个标题才能使其正常工作?
答案 0 :(得分:0)
@ blade19899
更改此行:
发件人:
curl_setopt($ch, CURLOPT_USERPWD, "$USERNAME:$password");
收件人: curl_setopt($ch, CURLOPT_USERPWD, base64_encode("$USERNAME:$password"));
出于安全原因,授权标头中应使用base64字符串。希望对您有帮助。