Home > Backend Development > PHP Tutorial > How can I reliably test for 404 errors in my PHP scraping code?

How can I reliably test for 404 errors in my PHP scraping code?

Mary-Kate Olsen
Release: 2024-12-04 08:09:13
Original
869 people have browsed it

How can I reliably test for 404 errors in my PHP scraping code?

Testing URLs for 404 in PHP: A Comprehensive Guide

Finding that URLs return unexpected 404 errors can disrupt your scraping code. To address this, it's essential to implement a test at the beginning of your code to check for this specific status code.

fsockopen Approach

One suggested method involves using fsockopen(). However, if the URL has a redirect, fsockopen() may return an empty result for all values.

curl Approach

A more reliable approach utilizes PHP's curl bindings. With curl, you can retrieve the HTTP error code using curl_getinfo(). Here's an example:

$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);

$response = curl_exec($handle);

$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if ($httpCode == 404) {
    // Handle 404 error here
}

curl_close($handle);

// Handle the response as needed
Copy after login

This code initializes a curl handle for the specified $url, sets the option to return the response as a string, executes the request, and retrieves the HTTP code. If the code is 404, it navigates the appropriate error handling code.

Conclusion

By employing curl's curl_getinfo() function, you can effectively check for 404 errors in your PHP scraping code, preventing downstream disruptions and ensuring the stability of your data extraction process.

The above is the detailed content of How can I reliably test for 404 errors in my PHP scraping code?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template