Hello! I have this question. I send post data using cURL to another site. I need to check if a non-existent url of the site is entered in the curtain, then finally page 404 returns. How to check that page 404 is returned? Looking for a 404 substring in the adopted html or is there some other way?

1 answer 1

It is very individual for each site.

The "correct" 404 page gives http_code 404, but unfortunately not all sites are correct. Many give 200 OK to 404 error.

In addition, each site can make their custom 404 page with a unique design. There may not even meet 404 (easy: made a picture)

Therefore, you need to look at a specific site and analyze specifically its features. In addition, the adopted algorithm may suddenly stop working after the redesign of the site.

Looking in the body for only 404 is fraught with occasionally false positives - especially if there are a lot of different numbers and tables on the site - and there may be one where 404 is found.

Alas, there is no silver bullet. Pick up your own algorithm for a specific site.

PS The classic answer 404 in http_code is implemented as follows:

$handle = curl_init($url); curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE); /* Get the HTML or whatever is linked in $url. */ $response = curl_exec($handle); /* Check for 404 (file not found). */ $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); if($httpCode == 404) { /* Handle 404 here. */ } curl_close($handle); 

Example from here: https://stackoverflow.com/questions/408405/easy-way-to-test-a-url-for-404-in-php Also see the documentation: http://php.net/manual/ ru / function.curl-getinfo.php

  • I agree, either on the title to be tied up, or on the content of 404 pages (in case it gives 200 OK). - Invision
  • @AK I checked. The page displays the heading Status Code: 404 Not Found. How to get this header in php and process it? - aleks_sk
  • @aleks_sk Dig in the direction of curl_getinfo : add an example and link to the answer. - AK
  • Exactly what is needed! Thank! - aleks_sk