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

Laravel中使用Curl发起请求时应传入何种URL?

Fixing Your cURL Request: URL & Data Format Issues

First off, the biggest issue here is the relative URL you’re using. cURL requires an absolute, full URL (including protocol like http/https, domain, and full path) to send requests properly. Instead of /notification/server, you need something like https://your-api-domain.com/notification/server — replace your-api-domain.com with the actual domain of the server you’re sending to.

Next, let’s look at how you’re handling the data. Right now you’re encoding your array to JSON but wrapping it in a form-urlencoded parameter (data=...), which is likely incorrect unless the server explicitly expects a form field named data containing JSON. Here are two common correct approaches depending on what the server accepts:

1. If the server expects raw JSON payload

Most modern APIs expect JSON directly in the request body. Adjust your code like this:

// Prepare data
$data = [
    "code__client" => "VERTDIS13",
    "status" => "90",
    "numero__incident" => "INC544842"
];

// Convert to JSON
$jsonData = json_encode($data);

// Initialize cURL
$ch = curl_init();

// Set full absolute URL
curl_setopt($ch, CURLOPT_URL, "https://your-api-domain.com/notification/server");
curl_setopt($ch, CURLOPT_POST, true);

// Send raw JSON
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);

// Set correct Content-Type header for JSON
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Content-Type: application/json",
    "Content-Length: " . strlen($jsonData)
]);

// Optional: Capture the response instead of printing it directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

// Check for cURL errors
if(curl_errno($ch)) {
    echo 'Curl error: ' . curl_error($ch);
}

// Clean up cURL resource
curl_close($ch);

// Handle the response
var_dump($response);

2. If the server expects form-urlencoded data

If the server accepts standard form data (like a regular HTML form), skip the JSON encoding and use http_build_query() instead:

// Prepare data
$data = [
    "code__client" => "VERTDIS13",
    "status" => "90",
    "numero__incident" => "INC544842"
];

// Encode as form data
$formData = http_build_query($data);

// Initialize cURL
$ch = curl_init();

// Set full absolute URL
curl_setopt($ch, CURLOPT_URL, "https://your-api-domain.com/notification/server");
curl_setopt($ch, CURLOPT_POST, true);

// Send form data
curl_setopt($ch, CURLOPT_POSTFIELDS, $formData);

// Optional: Capture response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

// Error handling
if(curl_errno($ch)) {
    echo 'Curl error: ' . curl_error($ch);
}

curl_close($ch);

var_dump($response);

Key Tips:

  • Always use absolute URLs: cURL can’t resolve relative paths on its own reliably, so stick to full URLs including protocol and domain.
  • Match Content-Type to your data: Make sure the Content-Type header matches the format you’re sending (application/json for JSON, application/x-www-form-urlencoded for form data).
  • Add error handling: Checking for cURL errors will save you time debugging failed requests.

If you’re still unsure about the correct URL, refer to the API documentation for the service you’re interacting with, or ask the server administrator for the exact endpoint URL.

内容的提问来源于stack exchange,提问作者Gautier Drincqbier

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:45:28