使用CURL访问带路径参数页面返回空白页问题求助
I've been using CURL to make requests lately, but I've noticed it returns a blank page when I add a text parameter to the path (e.g.,
/kenteken/TF172H). Accessing the main page/kenteken/works without any issues. I need technical help figuring this out. Here's my implementation code:
$ktk = $_GET['kenteken']; // create curl resource $ch = curl_init(); // set url curl_setopt($ch, CURLOPT_URL, "https://finnik.nl/kenteken/" . $ktk); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); curl_setopt($ch, CURLOPT_REFERER, "http://www.google.com"); curl_setopt($ch, CURLOPT_AUTOREFERER, true); // $output contains the output string $output = curl_exec($ch); // close curl resource to free up system resources curl_close($ch); print_r($output);
Troubleshooting & Fixes
Blank CURL responses almost always mean silent failures—let's break down how to diagnose and fix this step by step:
1. First, Enable CURL Error Reporting
Your code doesn't check for CURL-specific errors right now, which is the fastest way to get clarity. Add these lines immediately after curl_exec($ch):
if(curl_errno($ch)) { echo 'Curl error: ' . curl_error($ch); }
This will tell you exactly what's going wrong—whether it's an SSL issue, connection timeout, or something else.
2. Check the HTTP Response Code
Even if CURL doesn't throw an error, the server might be returning a 403 Forbidden, 404 Not Found, or 500 Internal Server Error. Add this line to get the status code:
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); echo 'HTTP Status Code: ' . $httpCode;
A 403 is a common sign the server is blocking your request (anti-scraping measures are likely the culprit here).
3. Update Your Request Headers
Your current User-Agent is extremely outdated (MSIE 6.0)—modern servers block these because they're linked to scrapers or obsolete browsers. Swap it for a recent, standard one:
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36");
Also, add headers that real browsers send to blend in:
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language: en-US,en;q=0.5' ));
And change your referrer to the actual site's root path instead of Google—this looks more natural:
curl_setopt($ch, CURLOPT_REFERER, "https://finnik.nl/kenteken/");
4. Handle SSL Correctly
Silent failures can happen if CURL hits an SSL certificate issue. Add these options to ensure proper SSL handling:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
If you get SSL errors during testing, you can temporarily disable verification (not recommended for production):
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
5. URL-Encode Your Parameter
While your license plate example doesn't have special characters, it's a good practice to URL-encode the parameter to avoid formatting issues:
$ktk = urlencode($_GET['kenteken']);
6. Simulate a Browser Session
Some servers require cookies to serve dynamic content. Add these lines to handle cookie storage/reuse:
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt'); curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
Full Updated Code
Here's all the fixes rolled into one working snippet:
$ktk = urlencode($_GET['kenteken']); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://finnik.nl/kenteken/" . $ktk); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36"); curl_setopt($ch, CURLOPT_REFERER, "https://finnik.nl/kenteken/"); curl_setopt($ch, CURLOPT_AUTOREFERER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language: en-US,en;q=0.5' )); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt'); curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt'); $output = curl_exec($ch); // Debug checks if(curl_errno($ch)) { echo 'Curl error: ' . curl_error($ch); } $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); echo 'HTTP Status Code: ' . $httpCode; curl_close($ch); print_r($output);
Start with enabling error reporting first—that will give you the most direct insight into what's blocking your request. Odds are, the outdated User-Agent was the main issue here.
内容的提问来源于stack exchange,提问作者Daxter112

