[EDIT]: I fixed it by installing cURL for PHP on my server.
I'm currently developing an API and have successfully implemented it using HTML and JavaScript. However, when I attempt to convert it to PHP for server-side processing, it doesn't seem to function properly. I need all requests to be handled server-side. I can't know what is wrong since I don't know how to access the php logs. When I make a POST request to the php page, I get a 500 Internal Server Error.
My Html code :
<!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>
And the 'same but non-working code' in 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;
}
?>
When I make a POST request to the php page, I get a 500 Internal Server Error. The only thing that seems to work is sending the $output as the response to the request. I think it might be a problem with how the request are made.