Hello. If you enter a non-existent site file_get_contents gives 500 error.

Question: I have a list of URLs, how can I sort them into workers and not if I get a 500 error?

This design does not help.

$opts = array('http' => array( 'method' => "GET", 'header' => "User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0\r\n" . "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" . "Accept-Encoding:gzip, deflate\r\n" . "Accept-Language:cs,en-us;q=0.7,en;q=0.3\r\n" . "Connection:keep-alive\r\n" )); $context = stream_context_create($opts); $link=file_get_contents("http://test12321434.ru/",false,$context); 

    1 answer 1

    Based on the answers: here and here .

    We are trying to get the content of the page, $content = @file_get_contents($site); , @ symbol, if you do not need to display warning warnings.

    Next, we check if we could read the content if($content === FALSE) { // Здесь обрабатываем ошибку... }

    If you want to check the headers (for code 500 for example), use the global variable $http_response_header .

    How to divide the links already decide.

    Although with cURL I think it will look better.

    He gave an example below how you can use all of the above.

     $urls = [ 'http://site1.ru', 'http://site2.ru', ] $badUrls = []; $goodUrls = []; foreach ($urls as $url) { $content = @file_get_contents($site); if($content === FALSE) { //Здесь обрабатываем ошибку, //можно проверить заголовки из $http_response_header array_push($badUrls, url); } else { array_push($goodUrls, url); } } 
    • Thanks for the answer. But you somehow did not complicate my question! Interested in solving the problem of why adding a non-working link, I get a 500 response - Sarkis Allahverdian
    • @Sarkis Allahverdian, then a very strange question :-) get the answer 500 because the link is not working :-) - Evgenii Izhboldin 6:43