新手求助:如何将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-dataorx-www-form-urlencoded, you'll see a list of key-value pairs—these are exactly what needs to go intoCURLOPT_POSTFIELDS. - If you're sending raw JSON, copy the entire JSON string directly, and make sure your
content-typeheader matches (more on that below).
- If you're using
- Next, check the Headers tab:
- Remove the
Postman-Tokenheader—this is a Postman-specific identifier that the server doesn't need. - Verify your
Authorization: Basicstring: it should be the Base64-encoded version ofyour_username:your_password(you can generate this in PHP withbase64_encode('username:password')if needed). - Match the
content-typeheader to your Body format:- For form data:
application/x-www-form-urlencodedormultipart/form-data - For raw JSON:
application/json
- For form data:
- Remove the
第二步:修正你的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
相关产品推荐
相关产品推荐

