JavaScript转PHP时Web请求出现500内部服务器错误求助
问题与解决
[编辑]: 已通过在服务器上安装PHP的cURL扩展解决该问题。
我正在开发一套API功能,用HTML+JavaScript实现时一切正常,但转成PHP做服务器端处理后无法运行。要求所有请求都在服务器端完成,但我不知道怎么查看PHP日志,没法定位问题。向PHP页面发送POST请求时,会收到500内部服务器错误。目前只有返回$output的功能正常,怀疑问题出在API请求的处理环节。
我的HTML代码
<!DOCTYPE html> <html> <head> <title>Execute Command</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script> $(document).ready(function () { $("form").submit(function (event) { event.preventDefault(); // Execute the shell command $.ajax({ type: "POST", url: "Speech_To_Text.php", success: function (output) { // Make the API request to Shuttle AI var apiKey = "APIKEY"; var apiUrl = "https://api.shuttleai.app/v1/chat/completions"; var requestData = { // model: "shuttle-1", model: "shuttle-1", internet: true, messages: [ { role: "user", content: output } ] }; // Send the request using fetch API fetch(apiUrl, { method: 'POST', headers: { 'Authorization': 'Bearer ' + apiKey, 'Content-Type': 'application/json' }, body: JSON.stringify(requestData) }) .then(response => response.json()) .then(data => { // Extract the content of the message var messageContent = data.choices[0].message.content; // Log the content in the console console.log(messageContent); // Make the audio generation request var audioGenerationOptions = { method: 'POST', headers: { Authorization: 'Bearer ' + apiKey, 'Content-Type': 'application/json' }, body: JSON.stringify({ input: messageContent, voice: 'thomas', model: 'eleven-labs' }) }; fetch('https://api.shuttleai.app/v1/audio/generations', audioGenerationOptions) .then(response => response.json()) .then(audioResponse => { // Extract the content of the message var AudioURL = audioResponse.data.url; // Log the content in the console console.log(AudioURL); }) }) .catch(error => console.error('Error:', error)); }, error: function (xhr, status, error) { console.error("Error:", error); } }); }); }); </script> </head> <body> <form> <input type="submit" value="Execute Command"> </form> </body> </html>
功能异常的PHP代码
<?php // Ensure that the form is submitted if ($_SERVER["REQUEST_METHOD"] == "POST") { $output = shell_exec("php Speech_To_Text.php"); // Make the API request to Shuttle AI $apiKey = "APIKEY"; $apiUrl = "https://api.shuttleai.app/v1/chat/completions"; $requestData = array( "model" => "shuttle-1", "internet" => true, "messages" => array( array( "role" => "user", "content" => $output ) ) ); // Send the request using cURL $ch = curl_init($apiUrl); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requestData)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer ' . $apiKey, 'Content-Type: application/json' )); $apiResponse = curl_exec($ch); curl_close($ch); // Decode the API response $apiData = json_decode($apiResponse, true); // Extract the content of the message $messageContent = $apiData['choices'][0]['message']['content']; echo $messageContent; // Make the audio generation request $audioGenerationOptions = array( "input" => $messageContent, "voice" => "thomas", "model" => "eleven-labs" ); $audioGenerationUrl = "https://api.shuttleai.app/v1/audio/generations"; $audioGenerationOptions = json_encode($audioGenerationOptions); // Send the audio generation request using cURL $ch2 = curl_init($audioGenerationUrl); curl_setopt($ch2, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch2, CURLOPT_POSTFIELDS, $audioGenerationOptions); curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch2, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer ' . $apiKey, 'Content-Type: application/json' )); $audioResponse = curl_exec($ch2); curl_close($ch2); // Decode the audio generation response $audioData = json_decode($audioResponse, true); // Extract the URL of the audio $audioUrl = $audioData['data']['url']; echo $audioUrl; } ?>
内容的提问来源于stack exchange,提问作者Loic Daigle
相关产品推荐
相关产品推荐

