0

I have a php numeric array that includes 1000+ external links. Can you please help me figure out a PHP or JS function that will loop through every link and check if it's working or not? To create an array and include links that are no longer working.

For now I'm using this code:

$links = array(
    'http://google.com',
    'http://example.com',
    'http://awkrlalrno1in01n2rn12r12r.com',
    'http://112om1om1om.ru'
);

foreach($links as $link) {
    if($file_headers = @get_headers($link)) {
        if(strpos($file_headers[0],'404') !== false) {
            $toDeleteLinks[] = $link;
        }
    }elseif($handle = curl_init($link)) {
        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 == 0) {
            $toDeleteLinks[] = $link;
        }
        curl_close($handle);
    }
}

if(isset($toDeleteLinks)) {
    echo '<pre>';
    print_r($toDeleteLinks);
    echo '</pre>';
}

but it gives a 500 Internal Server Error.. seems like server is unable to handle so much requests, since I've tried to create an array with 4 links, 2 - correct links, 2 incorrect ones and the function works as a charm.

I'm about to pull my hears out of my head, so please help :D Thanks in advance!

Beqa
  • 101
  • 2
  • 14
  • Um... is the code at that link your code? If so why not put it in this post? – nerdlyist May 26 '16 at 17:33
  • Sorry, that's my fault, but anyway, do you have any solutions? – Beqa May 26 '16 at 18:32
  • What does your php error log say why the process failed? If the maximum execution time is reached you could simply extend it in the loop by `set_time_limit(30);` for each request you are doing – NiMeDia May 26 '16 at 19:12
  • 1
    Check the actual errors instead of asking the community to guess what could be wrong. – PeeHaa May 26 '16 at 20:18
  • Here is an error I obtained at error_log file: PHP Warning: get_headers('http ://112om1om1om.ru'): failed to open stream: php_network_getaddresses: getaddrinfo failed – Beqa May 26 '16 at 21:03
  • @Beqa that warning should not lead to error 500 - there should be something after that – NiMeDia May 27 '16 at 14:51
  • There is nothing after that, I just get an error, that's all – Beqa Jun 14 '16 at 13:11

0 Answers0