如何通过file_get_html设置Cookie?获取对应国家定价数据求助
Yes, you can absolutely work around this issue to get the correct region-specific pricing! The problem is that file_get_html (from the Simple HTML DOM Parser) doesn’t have a built-in cookie setter, but we can inject cookies into the request using PHP’s stream contexts or switch to cURL for more control. Here’s your step-by-step guide:
1. Identify the Required Cookie
First, you need to know exactly what cookie the site sets when you select a country. Do this in your browser:
- Go to the site, select your desired country, and let the page reload.
- Open your browser’s DevTools (F12), go to the Application tab, then expand Cookies for the site.
- Look for cookies related to country/region (e.g.,
country_code,region,currency). Note their names and values (e.g.,country_code=DEfor Germany).
2. Use Stream Contexts with file_get_html
You can pass a custom stream context to file_get_html that includes the Cookie header. Here’s an example:
// Replace with your actual cookie data from DevTools $cookieString = "country_code=US; currency=USD"; // Create a stream context with the Cookie header $context = stream_context_create([ 'http' => [ 'header' => "Cookie: $cookieString\r\n" ] ]); // Fetch the HTML with the custom cookie $html = file_get_html('https://your-target-site.com/pricing', false, $context); // Now parse the country-specific pricing as usual if ($html) { // Replace with your actual selector for price elements $priceElements = $html->find('.product-price'); foreach ($priceElements as $price) { echo "Price: " . $price->plaintext . "<br>"; } }
3. Alternative: Use cURL (More Reliable for Session Handling)
If the site requires you to first submit a country selection form (instead of just setting a cookie directly), cURL is better because it can handle session cookies automatically. Here’s how to do it:
// Step 1: Submit the country selection form to get the session cookie $ch = curl_init('https://your-target-site.com/set-country'); curl_setopt($ch, CURLOPT_POST, true); // Replace with the form fields the site uses (check DevTools > Network tab) curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([ 'country' => 'CA', 'submit' => 'Select' ])); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_COOKIEJAR, 'temp_cookies.txt'); // Save cookies to a temp file curl_exec($ch); curl_close($ch); // Step 2: Fetch the pricing page using the saved cookies $ch = curl_init('https://your-target-site.com/pricing'); curl_setopt($ch, CURLOPT_COOKIEFILE, 'temp_cookies.txt'); // Load the saved cookies curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $htmlContent = curl_exec($ch); curl_close($ch); // Load the HTML into Simple HTML DOM for parsing $html = str_get_html($htmlContent); // Parse the prices if ($html) { $price = $html->find('.main-price', 0)->plaintext; echo "Canada-specific price: $price"; } // Clean up the temp cookie file unlink('temp_cookies.txt');
Key Notes
- Always make sure the cookie values match exactly what the site uses (case-sensitive!).
- Some sites might use multiple cookies (e.g., one for country, one for currency), so include all relevant ones in the header.
- If the site uses JavaScript to set cookies, you might need to use a headless browser tool like Puppeteer, but most pricing sites rely on server-side cookie checks which the above methods handle.
内容的提问来源于stack exchange,提问作者MUHSIN MOHAMED PC

