本地主机cURL脚本问题:获取的目标网页内容不完整
Hey there, let's figure out why your cURL request is returning incomplete content for that BLS visa page. I've run into similar issues with heavily protected or dynamic sites before, so here are targeted fixes to try:
1. Mimic a Real Browser with Proper HTTP Headers
Many sites block or truncate requests that don't look like they're coming from a legitimate browser. Add these headers to make your request more authentic:
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', 'Accept-Encoding: gzip, deflate, br', 'Referer: https://morocco.blsspainvisa.com/', 'DNT: 1', 'Connection: keep-alive', 'Upgrade-Insecure-Requests: 1' ));
2. Handle Compressed Content
If the server returns gzip/deflate compressed content (which many sites do to save bandwidth), cURL won't automatically decompress it by default—leading to truncated or garbled text. Add this line to fix it:
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
3. Increase Timeout Limits
Slow network connections or server delays can cause cURL to cut off the request early. Extend the timeout values:
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); // Time to establish connection (seconds) curl_setopt($ch, CURLOPT_TIMEOUT, 60); // Total request timeout (seconds)
4. Follow Redirects
Some sites redirect to other pages before serving content, and cURL doesn't follow these by default. Enable redirect following:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_MAXREDIRS, 5); // Limit redirects to avoid loops
5. Add Error Checking
Always check for cURL errors after execution—this will help you spot issues like connection failures or SSL problems:
if(curl_errno($ch)){ echo 'Curl error: ' . curl_error($ch); }
Full Modified Script
Here's the complete code with all the fixes applied:
<?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://morocco.blsspainvisa.com/embassy_book_appointment.php'); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Add browser-like headers 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', 'Accept-Encoding: gzip, deflate, br', 'Referer: https://morocco.blsspainvisa.com/', 'DNT: 1', 'Connection: keep-alive', 'Upgrade-Insecure-Requests: 1' )); // Handle compressed content curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate'); // Increase timeouts curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($ch, CURLOPT_TIMEOUT, 60); // Follow redirects curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_MAXREDIRS, 5); // Fallback User-Agent if server variable isn't available $userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'; curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); $page = curl_exec($ch); // Check for cURL errors if(curl_errno($ch)){ echo 'Curl error: ' . curl_error($ch); } curl_close($ch); echo $page; ?>
If It Still Doesn't Work
If the content is still incomplete, the site might be using JavaScript to render dynamic content (like React/Vue). cURL only fetches static HTML, so you'd need a tool like Puppeteer or Selenium to simulate a full browser that executes JS. But start with the fixes above—they resolve most cases of truncated content from protected sites.
内容的提问来源于stack exchange,提问作者npolio

