Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8876001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:06:46+00:00 2026-06-14T19:06:46+00:00

I’m using a PHP script (using cURL) to check whether: The links in my

  • 0

I’m using a PHP script (using cURL) to check whether:

  • The links in my database are correct (ie return HTTP status 200)
  • The links are in fact redirected and redirect to an appropriate/similar page (using the contents of the page )

The results of this are saved to a log file and emailed to me as an attachment.

This is all fine and working, however it is slow as all hell and half the time it times out and aborts itself early. Of note, I have about 16,000 links to check.

Was wondering how best to make this run quicker, and what I’m doing wrong?

Code below:

function echoappend ($file,$tobewritten) {

        fwrite($file,$tobewritten);
        echo $tobewritten;
}

error_reporting(E_ALL);
ini_set('display_errors', '1');


$filename=date('YmdHis') . "linkcheck.htm";
echo $filename;
$file = fopen($filename,"w+");

try {
        $conn = new PDO('mysql:host=localhost;dbname=databasename',$un,$pw);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        echo '<b>connected to db</b><br /><br />';

        $sitearray = array("medical.posterous","ebm.posterous","behavenet","guidance.nice","www.rch","emedicine","www.chw","www.rxlist","www.cks.nhs.uk");

        foreach ($sitearray as $key => $value) {    
            $site=$value;

            echoappend ($file, "<h1>" . $site . "</h1>");

            $q="SELECT * FROM link WHERE url LIKE :site";
            $stmt = $conn->prepare($q);
            $stmt->execute(array(':site' => 'http://' . $site . '%'));
            $result = $stmt->fetchAll();

            $totallinks = 0;
            $workinglinks = 0;

            foreach($result as $row)
            {

                $ch = curl_init();
                $originalurl = $row['url'];

                curl_setopt($ch, CURLOPT_URL, $originalurl);
                curl_setopt($ch, CURLOPT_HEADER, 1);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_NOBODY, true);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);


                $output = curl_exec($ch);
                if ($output === FALSE) {
                    echo "cURL Error: " . curl_error($ch);
                }

                $urlinfo = curl_getinfo($ch);

                if ($urlinfo['http_code'] == 200)
                {
                    echoappend($file, $row['name'] . ": <b>working!</b><br />");
                    $workinglinks++;
                }
                else if ($urlinfo['http_code'] == 301 || 302)
                {
                    $redirectch = curl_init();                  
                    curl_setopt($redirectch, CURLOPT_URL, $originalurl);
                    curl_setopt($redirectch, CURLOPT_HEADER, 1);
                    curl_setopt($redirectch, CURLOPT_RETURNTRANSFER, 1);
                    curl_setopt($redirectch, CURLOPT_NOBODY, false);
                    curl_setopt($redirectch, CURLOPT_FOLLOWLOCATION, true);

                    $redirectoutput = curl_exec($redirectch);

                    $doc = new DOMDocument();
                    @$doc->loadHTML($redirectoutput);
                    $nodes = $doc->getElementsByTagName('title');

                    $title = $nodes->item(0)->nodeValue;

                    echoappend ($file, $row['name'] . ": <b>redirect ... </b>" . $title . " ... ");

                    if (strpos(strtolower($title),strtolower($row['name']))===false) {
                        echoappend ($file, "FAIL<br />");
                    }
                    else {
                        $header = curl_getinfo($redirectch);
                        echoappend ($file, $header['url']);
                        echoappend ($file, "SUCCESS<br />");
                    }

                    curl_close($redirectch);
                }
                else
                {
                    echoappend ($file, $row['name'] . ": <b>FAIL code</b>" . $urlinfo['http_code'] . "<br />");
                }

                curl_close($ch);

                $totallinks++;
            }
            echoappend ($file, '<br />');

            echoappend ($file, $site . ": " . $workinglinks . "/" . $totallinks . " links working. <br /><br />");


        }

        $conn = null;
        echo '<br /><b>connection closed</b><br /><br />';

    } catch(PDOException $e) {
            echo 'ERROR: ' . $e->getMessage();
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:06:46+00:00Added an answer on June 14, 2026 at 7:06 pm

    Short answer is use the curl_multi_* methods to parallelize your requests.

    The reason for the slowness is that web requests are comparatively slow. Sometimes VERY slow. Using the curl_multi_* functions lets you run multiple requests simultaneously.

    One thing to be careful about is to limit the number of requests you run at once. In other words, don’t run 16,000 requests at once. Maybe start at 16 and see how that goes.

    The following example should help you get started:

    <?php
    
    //
    // Fetch a bunch of URLs in parallel. Returns an array of results indexed
    // by URL.
    //
    function fetch_urls($urls, $curl_options = array()) {
      $curl_multi = curl_multi_init();
      $handles = array();
    
      $options = $curl_options + array(
        CURLOPT_HEADER         => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_NOBODY         => true,
        CURLOPT_FOLLOWLOCATION => true);
    
      foreach($urls as $url) {
        $handles[$url] = curl_init($url);
        curl_setopt_array($handles[$url], $options);
        curl_multi_add_handle($curl_multi, $handles[$url]);
      }
    
      $active = null;
      do {
        $status = curl_multi_exec($curl_multi, $active);
      } while ($status == CURLM_CALL_MULTI_PERFORM);
    
      while ($active && ($status == CURLM_OK)) {
        if (curl_multi_select($curl_multi) != -1) {
          do {
            $status = curl_multi_exec($curl_multi, $active);
          } while ($status == CURLM_CALL_MULTI_PERFORM);
        }
      }
    
      if ($status != CURLM_OK) {
        trigger_error("Curl multi read error $status\n", E_USER_WARNING);
      }
    
      $results = array();
      foreach($handles as $url => $handle) {
        $results[$url] = curl_getinfo($handle);
        curl_multi_remove_handle($curl_multi, $handle);
        curl_close($handle);    
      }
      curl_multi_close($curl_multi);
    
      return $results;
    }
    
    //
    // The urls to test
    //
    $urls = array("http://google.com", "http://yahoo.com", "http://google.com/probably-bogus", "http://www.google.com.au");
    
    //
    // The number of URLs to test simultaneously
    //
    $request_limit = 2;
    
    //
    // Test URLs in batches
    //
    $redirected_urls = array();
    for ($i = 0 ; $i < count($urls) ; $i += $request_limit) {
      $results = fetch_urls(array_slice($urls, $i, $request_limit));
      foreach($results as $url => $result) {
        if ($result['http_code'] == 200) {
          $status = "Worked!";
        } else {
          $status = "FAILED with {$result['http_code']}";
        }
        if ($result["redirect_count"] > 0) {
          array_push($redirected_urls, $url);
          echo "{$url}: ${status}\n";
        } else {
          echo "{$url}: redirected to {$result['url']} and {$status}\n";
        }
      }
    }
    
    //
    // Handle redirected URLs
    //
    echo "Processing redirected URLs...\n";
    for ($i = 0 ; $i < count($redirected_urls) ; $i += $request_limit) {
      $results = fetch_urls(array_slice($redirected_urls, $i, $request_limit), array(CURLOPT_FOLLOWLOCATION => false));
      foreach($results as $url => $result) {
        if ($result['http_code'] == 301) {
          echo "{$url} permanently redirected to {$result['url']}\n";
        } else if ($result['http_code'] == 302) {
          echo "{$url} termporarily redirected to {$result['url']}\n";
        } else {
          echo "{$url}: FAILED with {$result['http_code']}\n";
        }
      }
    }
    

    The above code processes a list of URLs in batches. It works in two passes. In the first pass, each request is configured to follow redirects and simply reports whether each URL ultimately lead to a successful request, or a failure.

    The second pass processes any redirected URLs detected in the first pass and reports whether the redirect was a permanent redirection (meaning you can update your database with the new URL), or temporary (meaning you should NOT update your database).

    NOTE:

    In your original code, you have the following line, which will not work the way you expect it to:

    else if ($urlinfo['http_code'] == 301 || 302)
    

    The expression will ALWAYS return TRUE. The correct expression is:

    else if ($urlinfo['http_code'] == 301 || $urlinfo['http_code'] == 302)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.