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

如何使用cURL提交包含单选按钮的表单?代码求助

Fixing Your cURL Form Submission Request

Hey Abdul, let's get that server-side form submission working properly! I spotted a key mismatch in your current code that's likely causing issues—let's break it down and fix it step by step.

The Core Problem

You're sending a JSON string as your POST data, but you've set the Content-Type header to application/x-www-form-urlencoded. These two formats don't match, so the server can't correctly parse your form data.

Solution 1: Use x-www-form-urlencoded Format (Most Common for Basic Forms)

If the server expects standard form-encoded data (like a regular HTML form submission), adjust your POST data to use key-value pairs instead of JSON:

$url = "Url goes here";
$ch = curl_init();

// Set basic cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);

// Encode form data correctly for x-www-form-urlencoded
$postData = http_build_query([
    'postcode' => '3150',
    'customerType' => 'Residential',
    'fuelType' => 'Electricity'
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

// Set matching Content-Type header
$headers = [
    "Content-Type: application/x-www-form-urlencoded"
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// Execute request and handle errors
$response = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Request Error: ' . curl_error($ch);
}
curl_close($ch);

// Inspect the server response
var_dump($response);

Solution 2: Use JSON Format (If Server Accepts It)

If the server is designed to receive JSON data, keep your JSON payload but update the Content-Type header to match:

$url = "Url goes here";
$ch = curl_init();

// Set basic cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);

// Encode data as JSON
$postData = json_encode([
    'postcode' => '3150',
    'customerType' => 'Residential',
    'fuelType' => 'Electricity'
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

// Set JSON Content-Type header
$headers = [
    "Content-Type: application/json"
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// Execute request and handle errors
$response = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Request Error: ' . curl_error($ch);
}
curl_close($ch);

// Inspect the server response
var_dump($response);

Quick Tips to Avoid Issues

  • Always confirm the server's expected Content-Type (check the API docs or use browser dev tools to see how the original form submits data).
  • Use http_build_query (for form-encoded) or json_encode (for JSON) to handle special character encoding automatically—this prevents bugs from manual string concatenation.
  • Add error handling with curl_errno to catch issues like connection failures or invalid URLs quickly.

内容的提问来源于stack exchange,提问作者Abdul Rahman

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:20:46