0

1) I am using wordpress engine. 2) I have an numeric array() with 800+ links in it, like this.

What I'm trying to do is to run foreach() function and check if link still exist (not returning 404 error).

I tried 2 functions:

1)

<?php
foreach($links as $link) {
    $file_headers = @get_headers($link);
    if(strpos($file_headers[0],'404') === false) {
        $toDeleteLinks[] = $link;
    }
}
?>

so according to this first function, $toDeleteLinks array should contain all the links that return 404 error. using get_headers() functions here...

2)

<?php
foreach($links as $link) {
    $handle = curl_init($link);
    curl_setopt($handle,  CURLOPT_RETURNTRANSFER, TRUE);
    $response = curl_exec($handle);
    $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
    if($httpCode != 404) {
        $toDeleteLinks[] = $link;
    }
    curl_close($handle);
}
?>

this second one should do the same just using cURL..

BUT in both cases I get redirected to the wordpress 404.php page ((. I think that's because of a big number of links.

Can you please help me get a solution for this? Use another function instead or however...

Thanks.

Beqa
  • 101
  • 2
  • 14
  • First make sure that your assumption about reason of 404 is true (I suppose it is not). Check if any of those loops is at all executed by WP or 404 happens before that. – michaJlS May 24 '16 at 21:13
  • Hey, thanks for you answer. I've tried to create an array with 4 links, 2 - correct links, 2 incorrect ones. So it works as a charm. But when I try to loop through an array $links that includes up to 900 links.. it gives a 500 Internal Server Error. – Beqa May 26 '16 at 16:43
  • here is a code I'm using http://pastebin.com/s9EFF7Au – Beqa May 26 '16 at 16:49
  • You need to check errors log and see what is the exact cause of that problem. Without that it will be difficult to debug that problem and find any solution. – michaJlS May 26 '16 at 18:17

0 Answers0