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

如何在PHP Laravel 5.5中正确配置CURL请求

Fixing Your Laravel CURL POST Request

Let me help you sort out this curl issue in your Laravel controller! Since your Postman request works perfectly but your code isn't giving the same result, let's break down the common mistakes and fix it step by step.

First, here are the most likely issues with your original code:

  • Missing the required x-api-key header in your curl options
  • Not properly encoding the request body (Postman handles this automatically, but curl needs explicit setup)
  • Forgetting to enable CURLOPT_RETURNTRANSFER to capture the response instead of printing it directly
  • No error handling to debug what's going wrong

Here's the corrected, complete version of your controller method:

public function curlPost() {
    $data1 = [
        'sub_domain' => 'value_1',
    ];

    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL => "https://uwxcdwsc0k.execute-api.us-east-1.amazonaws.com/your-full-endpoint", // Replace with your complete API URL
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => json_encode($data1), // Encode data to match Postman's JSON body
        CURLOPT_HTTPHEADER => array(
            "x-api-key: ZYWHUYAOSYSOASYYY", // Add your required API key header
            "Content-Type: application/json" // Critical for JSON-based APIs
        ),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);

    curl_close($curl);

    // Debug and return results
    if ($err) {
        return response()->json(['error' => 'cURL Error #:' . $err], 500);
    } else {
        return response()->json(['response' => json_decode($response, true)]);
    }
}

Key Fixes Explained:

  1. Added the x-api-key Header: I included your API key directly in the CURLOPT_HTTPHEADER array to match exactly what you're sending in Postman.
  2. JSON-Encoded Request Body: Used json_encode($data1) to format the payload correctly, paired with the Content-Type: application/json header—this is almost certainly what was missing, since Postman sends this header automatically when you use a raw JSON body.
  3. Enabled CURLOPT_RETURNTRANSFER: This tells curl to return the response as a string instead of printing it immediately, so you can process and return it in your Laravel response.
  4. Added Error Handling: The curl_error($curl) check will catch any cURL-specific issues (like connection timeouts or invalid URLs) so you can debug quickly if the request still fails.

Quick Verification Checks:

  • Make sure your full API URL is identical to the one in Postman (your original code was cut off, so double-check this!)
  • If your API expects form-data instead of JSON, replace json_encode($data1) with http_build_query($data1) and update the header to Content-Type: application/x-www-form-urlencoded
  • Re-test the endpoint in Postman to confirm it's still working as expected

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:00:42