You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何用PHP执行指定Curl命令?新手求助验证代码正确性

Converting Your Watson Speech-to-Text curl Command to PHP cURL

Hey there! No worries at all—let's turn that curl command into working PHP code, and I'll walk you through each part so you know exactly what's going on.

First, let's recap what your original curl command does: it sends a POST request to IBM Watson's Speech-to-Text API, authenticates with a username/password, sends a FLAC audio file, and includes some query parameters for timestamps, word alternatives, and keywords.

Here's the complete, working PHP code that matches your curl command:

// Replace these with your actual credentials and audio file path
$username = "{your-username}";
$password = "{your-password}";
$audioFilePath = "audio-file1.flac";

// Initialize cURL session
$ch = curl_init();

// Set the API endpoint URL with all query parameters
$url = "https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?timestamps=true&word_alternatives_threshold=0.9&keywords=%22colorado%22,%22tornado%22,%22tornadoes%22&keywords_threshold=0.5";
curl_setopt($ch, CURLOPT_URL, $url);

// Set authentication (matches the -u flag in curl)
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

// Set request method to POST
curl_setopt($ch, CURLOPT_POST, true);

// Set the Content-Type header (matches --header flag)
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Content-Type: audio/flac"
]);

// Read the audio file content and set as POST data (matches --data-binary "@file")
$audioContent = file_get_contents($audioFilePath);
if ($audioContent === false) {
    die("Error: Could not read audio file $audioFilePath");
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $audioContent);

// Tell cURL to return the response instead of printing it directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute the request and get the response
$response = curl_exec($ch);

// Check for cURL errors
if (curl_errno($ch)) {
    $errorMsg = curl_error($ch);
    die("cURL Error: $errorMsg");
}

// Close the cURL session
curl_close($ch);

// Process and print the response (it's JSON, so we can decode it)
$decodedResponse = json_decode($response, true);
print_r($decodedResponse);

Let's break down each key part to match your original curl command:

  • Authentication: The -u "{username}":"{password}" in curl maps to CURLOPT_USERPWD in PHP—we just pass the username and password separated by a colon.
  • Content-Type Header: The --header "Content-Type: audio/flac" becomes the CURLOPT_HTTPHEADER option, where we pass an array of headers.
  • Binary Audio Data: The --data-binary "@audio-file1.flac" is handled by reading the file's content with file_get_contents() and passing that content to CURLOPT_POSTFIELDS. This ensures the audio is sent as binary data, just like the curl command does.
  • Query Parameters: All the parameters in your curl URL (like timestamps=true) are included directly in the $url variable—no need to separate them out unless you want to build the URL dynamically later.
  • Return Response: CURLOPT_RETURNTRANSFER is crucial here—it tells cURL to give us the response as a string instead of printing it immediately, so we can process it.

Quick Checks to Make Sure It Works:

  1. Replace {your-username} and {your-password} with your actual IBM Watson credentials.
  2. Make sure audio-file1.flac is in the same directory as your PHP script, or use the full file path (like /var/www/audio/audio-file1.flac).
  3. If you get an error reading the file, check file permissions to ensure your PHP process can access it.

内容的提问来源于stack exchange,提问作者Äsiri Läkml

火山引擎 最新活动