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

新手求助:如何将Postman查询转为PHP cURL?遇认证与参数问题

关于Postman转PHP cURL获取JSON文件的问题解答

Hey there! Your judgment is totally spot-on—authentication failures and missing POST parameters are super common when translating Postman requests to PHP cURL. Let's break this down step by step to get you sorted.

第一步:从Postman中提取所需的POST参数和正确请求头

Since you already have a working Postman request (you mentioned you viewed the cURL code), here's how to dig out the exact details you need:

  • Open your successful Postman request, then switch to the Body tab:
    • If you're using form-data or x-www-form-urlencoded, you'll see a list of key-value pairs—these are exactly what needs to go into CURLOPT_POSTFIELDS.
    • If you're sending raw JSON, copy the entire JSON string directly, and make sure your content-type header matches (more on that below).
  • Next, check the Headers tab:
    • Remove the Postman-Token header—this is a Postman-specific identifier that the server doesn't need.
    • Verify your Authorization: Basic string: it should be the Base64-encoded version of your_username:your_password (you can generate this in PHP with base64_encode('username:password') if needed).
    • Match the content-type header to your Body format:
      • For form data: application/x-www-form-urlencoded or multipart/form-data
      • For raw JSON: application/json

第二步:修正你的PHP cURL代码

Here's an updated version of your code, with fixes for common issues and placeholders you can replace with your actual data. I'll assume your Postman request uses form data (adjust if you're using raw JSON):

$curl = curl_init();

// Replace these with your actual POST parameters from Postman's Body tab
$postParams = [
    'action' => 'fetch_events', // Example parameter—use your real keys/values
    'source' => 'api'
];

curl_setopt_array($curl, [
    CURLOPT_URL => "https://api.divessi.com/divessi/index.php",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    // Convert the parameter array to a URL-encoded string for form data
    CURLOPT_POSTFIELDS => http_build_query($postParams),
    CURLOPT_HTTPHEADER => [
        "Authorization: Basic YOUR_VALID_BASE64_STRING", // Replace with your correct Base64 credentials
        "Cache-Control: no-cache",
        "content-type: application/x-www-form-urlencoded" // Match your Body format
    ],
]);

$response = curl_exec($curl);
$obj = json_decode($response);
$events = [];

// Add null coalescing to avoid errors if the response structure is unexpected
if ($obj && isset($obj[0]->DATA)) {
    $events = $obj[0]->DATA;
}

$err = curl_error($curl);
curl_close($curl);

if ($err) {
    echo "cURL Error #: " . $err;
} else {
    // Debug tip: Print the raw response first to check for server-specific errors
    echo "<pre>";
    print_r($response);
    echo "</pre>";
    
    foreach ($events as $event) {
        // Your event processing logic here
    }
}

Pro Tips for Debugging

  • Use Postman's Code Generator: The easiest way to get accurate PHP cURL code is to open your working Postman request, click the Code button (top-right corner), then select PHP - cURL. Postman will generate ready-to-use code with all correct parameters and headers.
  • Check the Raw Response: If authentication still fails, print the raw $response—servers usually return descriptive errors like "Invalid credentials" or "Missing required parameter X" that will point you to the exact issue.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:54:26