使用CURL获取FIFA网页内容失败求助(附PHP代码)
Troubleshooting Your cURL Request to Sofifa
Let's break down the issues with your code and fix them step by step:
1. Truncated URL Syntax Error
Your curl_init call uses a truncated URL (&pn...), which is invalid. You need to pass the full, correct URL from your original request:
$ch = curl_init("https://sofifa.com/players?aeh=22&ptl=84&pth=99&pn=27&pn=25&pn=23");
2. Missing Essential cURL Options
Your function doesn't set critical cURL options that are required to successfully fetch the page:
CURLOPT_RETURNTRANSFER: Tells cURL to return the response as a string instead of printing it directly.CURLOPT_USERAGENT: Mimics a real browser request (many sites block requests without a valid user agent).CURLOPT_FOLLOWLOCATION: Follows any redirects the site might send.CURLOPT_CONNECTTIMEOUT/CURLOPT_TIMEOUT: Prevents hanging requests if the site is unresponsive.
3. Missing Request Execution & Error Handling
You aren't executing the cURL request with curl_exec, checking for errors, or closing the handle properly—all necessary steps to debug and complete the request.
Corrected Code Example
Here's the fixed version of your function with all necessary improvements:
function curl_download($url) { // Check if cURL is installed if (!function_exists('curl_init')) { die('Sorry cURL is not installed!'); } // Initialize cURL handle with full URL $ch = curl_init($url); // Set essential cURL options curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, '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_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_TIMEOUT, 10); // Execute the request $response = curl_exec($ch); // Check for cURL errors if (curl_errno($ch)) { $error_msg = curl_error($ch); curl_close($ch); die('cURL Error: ' . $error_msg); } // Close the cURL handle curl_close($ch); return $response; } // Usage $pageContent = curl_download("https://sofifa.com/players?aeh=22&ptl=84&pth=99&pn=27&pn=25&pn=23"); echo $pageContent;
Additional Notes on Anti-Scraping
Sofifa likely has anti-scraping measures in place. If the corrected code still doesn't work, you might need to:
- Add cookie handling with
CURLOPT_COOKIEJARandCURLOPT_COOKIEFILEto maintain session state. - Implement rate limiting (add delays between requests to avoid being blocked).
- Inspect the network requests in your browser's dev tools to mimic the exact headers sent by a real browser.
内容的提问来源于stack exchange,提问作者James Tyreece
相关产品推荐
相关产品推荐

