How do I check for valid (not dead) links programmatically using PHP?

Asked
Active3 hr before
Viewed126 times

6 Answers

using
90%

What is the best method for checking the status of a url in PHP considering both accuracy and performance?, Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers , Podcast 392: Do polyglots have an edge when it comes to mastering programming... , Advertising Reach developers & technologists worldwide

As a starting point you could use some function like this:

function is_available($url, $timeout = 30) {
   $ch = curl_init(); // get cURL handle

   // set cURL options
   $opts = array(CURLOPT_RETURNTRANSFER => true, // do not output to browser
      CURLOPT_URL => $url, // set URL
      CURLOPT_NOBODY => true, // do a HEAD request only
      CURLOPT_TIMEOUT => $timeout); // set timeout
   curl_setopt_array($ch, $opts);

   curl_exec($ch); // do it!

   $retval = curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200; // check if HTTP OK

   curl_close($ch); // close handle

   return $retval;
}
88%

Use the PHP cURL extension. Unlike fopen (), it can also make HTTP HEAD requests sufficient to check the availability of the URL and save a ton of bandwidth, since you do not need to download all the text of the page to check.,What is the scope of the finalizer - for the application domain or for each process? - multithreading,If you need more flexibility (e.g. timeout), check out the cURL extension.,Oh, and this code really strictly checks the HTTP 200 response code. It should not be forwarded (302) - but there is also the cURL option for this.

As a starting point, you can use some functions as follows:

function is_available($url, $timeout = 30) {
   $ch = curl_init(); // get cURL handle // set cURL options $opts = array(CURLOPT_RETURNTRANSFER => true, // do not output to browser CURLOPT_URL => $url, // set URL CURLOPT_NOBODY => true, // do a HEAD request only CURLOPT_TIMEOUT => $timeout); // set timeout curl_setopt_array($ch, $opts); curl_exec($ch); // do it! $retval = curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200; // check if HTTP OK curl_close($ch); // close handle return $retval; } 
72%

1.2.function is_available($url, $timeout = 30) {
      3. $ch = curl_init(); // get cURL handle4.5.    // set cURL options6.    $opts = array(CURLOPT_RETURNTRANSFER => true, // do not output to browser7.                  CURLOPT_URL => $url,            // set URL8.                  CURLOPT_NOBODY => true,         // do a HEAD request only9.                  CURLOPT_TIMEOUT => $timeout);   // set timeout10.    curl_setopt_array($ch, $opts); 11.12.    curl_exec($ch); // do it!13.14.    $retval = curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200; // check if HTTP OK15.16.    curl_close($ch); // close handle17.18.    return $retval;19.}20.
65%

'link' is emitted with the result of each checked/unskipped link (broken or not). Arguments: result is a Link. ,'link' is emitted with the result of each checked/unskipped link (broken or not) within the current page. Arguments: result is a Link. customData is whatever was queued. ,'link' is emitted for each checked/unskipped result (broken or not). Arguments: result is a Link. customData is whatever was queued. ,'junk' is emitted on each skipped/unchecked link, as configured in options. Arguments: result is a Link.

npm install broken - link - checker - g
load more v
75%

There are plenty of tools for crawling a website and reporting broken links.  You can get 90% of this with just wget or curl.,Here’s a perl tool that checks for dead links and more:,Using curl solves these particular problems, but requires libcurl to be built with your PHP, and it is quite clunky to use:, You are commenting using your Twitter account. ( Log Out /  Change )

The basic test is pretty simple

foreach($urllist as $url) {
   if (fopen($url)) {
      print "valid";
   }
}
40%

Try this: <?php echo do_shortcode(‘[shortcode]’); ?<,Sorry the code didn’t appear in the above instead of this: <?php echo do_shortcode(“[shortcode]”); ?<,But what if you want to use a shortcode from within a template instead of with the content of a Post/Page? You can invoke it with a special function:,php echo do_shortcode("[simple-staff-list]");

But what if you want to use a shortcode from within a template instead of with the content of a Post/Page? You can invoke it with a special function:

< ? php echo do_shortcode("[shortcode]"); ? >
load more v

Other "using-undefined" queries related to "How do I check for valid (not dead) links programmatically using PHP?"