CURL多Header GET请求失败问题求助(PHP新手)
Hey there! As someone who's wrestled with finicky cURL header issues early on, I totally get how frustrating it is when your request gets rejected or returns nothing. Let's break down the most likely problems and fix them step by step.
First: Fix Your Header Formatting
The biggest red flag here is your header names—HTTP headers use hyphens instead of spaces (e.g., User-Agent not User Agent). Servers strictly follow HTTP standards, so malformed header names will almost certainly cause an "unable to handle request" error.
Also, your lbcookie should be wrapped in the standard Cookie header, not sent as a standalone header.
Working Example Code
Here's a corrected, ready-to-use cURL script that includes all your required headers in the right format:
<?php // Replace with your actual target URL $targetUrl = "https://your-api-endpoint.com"; // Build headers with proper HTTP formatting $requestHeaders = [ "User-Agent: YourApp/1.0 (DeviceType; OSVersion; Language)", "Sso-Token: your-sso-token-here", "Subscriber-Id: your-subscriber-id", "Cookie: lbcookie=your-lbcookie-value", // Wrap lbcookie in Cookie header "Channel-Id: your-channel-id", "Srno: your-srno-value", "Unique-Id: your-unique-id", "Appkey: your-appkey-value", "Os-Version: your-os-version", "Os-Type: your-os-type", "Device-Type: your-device-type", "Device-Key: your-device-key", "Version-Code: your-version-code", "Language-Id: your-language-id", "Usergroup: your-usergroup", "Host: your-api-endpoint.com", // Must match the target domain exactly "Connection: keep-alive", "Accept-Encoding: gzip, deflate, br" ]; // Initialize cURL session $curl = curl_init($targetUrl); // Set critical cURL options curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Return response instead of printing it curl_setopt($curl, CURLOPT_HTTPHEADER, $requestHeaders); // Attach your headers curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // Follow redirects if needed curl_setopt($curl, CURLOPT_VERBOSE, true); // Enable verbose logging for debugging curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // Disable only for dev; enable in production! // Execute the request $response = curl_exec($curl); // Check for cURL errors if (curl_errno($curl)) { echo "cURL Error: " . curl_error($curl); } else { // Get HTTP status code to diagnose issues $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); echo "HTTP Status Code: " . $statusCode . "\n"; echo "Response: " . $response; } // Clean up cURL session curl_close($curl); ?>
Key Debugging Tips
Check the HTTP Status Code:
400 Bad Request: Your headers/request format is invalid (this is the most likely issue here)401 Unauthorized: Your SSO token or auth headers are incorrect500 Internal Server Error: The server has an issue (not your request)
Compare with Browser Requests:
Use Chrome DevTools (Network tab) to capture a working request from a browser, then compare its headers to your cURL headers. Look for missing values or formatting differences.Verbose Logging:
TheCURLOPT_VERBOSEoption will print exactly what cURL sends to the server. Use this to confirm all headers are being sent correctly.
If you still run into issues, share the verbose log output and HTTP status code—we can dig deeper!
内容的提问来源于stack exchange,提问作者Piyush

