You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求将指定curl命令转换为有效PHP代码(在线工具及exec方式均失败)

Got it, let's sort this out for you. The problem with using exec() is often tied to server environment quirks—like file path confusion, shell permission limits, or host restrictions. PHP's built-in cURL extension is way more reliable here, and it gives you better control over the request.

Solution: Convert Your cURL Command to PHP cURL Extension Code

Here's the working, equivalent PHP code that replicates your original cURL request properly:

<?php
// Initialize a cURL session
$curlSession = curl_init();

// Set the target upload URL
curl_setopt($curlSession, CURLOPT_URL, 'https://url/upload');

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

// Include response headers (matches the `-i` flag in your original cURL command)
curl_setopt($curlSession, CURLOPT_HEADER, true);

// Prepare multipart form data (this is the core of your file upload request)
$formData = [
    'account_id' => '12345',
    'authhash' => 'BKPda_T497AX4EjBsk3ttw9OcOzk',
    // Use CURLFile for secure, modern file uploads (replaces the old @ syntax)
    'audioFile' => new CURLFile('CasioVoice.wav', 'audio/wav', 'CasioVoice.wav')
];

curl_setopt($curlSession, CURLOPT_POSTFIELDS, $formData);

// Execute the request and capture the response
$response = curl_exec($curlSession);

// Handle errors if they occur
if (curl_errno($curlSession)) {
    echo 'cURL Error: ' . curl_error($curlSession);
} else {
    // Split headers and body (since we enabled CURLOPT_HEADER)
    $headerLength = curl_getinfo($curlSession, CURLINFO_HEADER_SIZE);
    $responseHeaders = substr($response, 0, $headerLength);
    $responseBody = substr($response, $headerLength);

    // Output or process the results as needed
    echo 'Response Headers:<br>' . nl2br($responseHeaders) . '<br><br>';
    echo 'Response Body:<br>' . $responseBody;
}

// Clean up the cURL session
curl_close($curlSession);
?>

Important Notes to Avoid Issues:

  • File Path Check: Make sure CasioVoice.wav is in the same directory as your PHP script, or use an absolute file path (e.g., /var/www/uploads/CasioVoice.wav) if it's stored elsewhere.
  • CURLFile Instead of @: Modern PHP versions (5.5+) deprecated the old @ syntax for file uploads. CURLFile is the secure, supported way to handle multipart file uploads.
  • Header Control: If you don't need the response headers (the -i flag in your original cURL), set CURLOPT_HEADER to false to simplify the response.
  • Error Debugging: The code includes error checking with curl_errno()—this will help you spot issues like connection failures or invalid file paths quickly.

Why Your exec() Approach Might Have Failed:

  • File Path Confusion: The shell running exec() might not be using the same working directory as your PHP script, so it can't find CasioVoice.wav.
  • Server Restrictions: Many web hosts disable exec() and similar shell functions for security reasons.
  • Missing Error Output: exec() only captures standard output—if cURL was throwing errors to standard error, you wouldn't see them. You'd need to add 2>&1 to the end of your command to redirect stderr to stdout (e.g., curl ... https://url/upload 2>&1), but this is still less reliable than using the cURL extension.

内容的提问来源于stack exchange,提问作者Maneesh Rao

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:49:17