Skip to content

Instantly share code, notes, and snippets.

@millipedia
Created September 10, 2021 10:13
Show Gist options
  • Select an option

  • Save millipedia/85782017a7ab45bbadfa8f0bc9867ab6 to your computer and use it in GitHub Desktop.

Select an option

Save millipedia/85782017a7ab45bbadfa8f0bc9867ab6 to your computer and use it in GitHub Desktop.

Revisions

  1. millipedia created this gist Sep 10, 2021.
    55 changes: 55 additions & 0 deletions check_links.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    <?php

    /**
    *
    * Read a CSV file of URLS then use curl
    * to check their http code and dump it all
    * out in a table.
    *
    */

    function page_404($url) {
    $handle = curl_init($url);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);

    /* Get the HTML or whatever is linked in $url. */
    $response = curl_exec($handle);

    // get the httpcode of the repsonse.
    $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
    curl_close($handle);

    return $httpCode;
    }

    echo '<html><body>';
    echo '<table><thead><tr><th>Page name</th><th>Response</th></tr></thead>';
    echo '<tbody>';

    if (($handle = fopen("urls_to_check.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

    $url=$data[0];

    $response=page_404($url);

    echo '<tr>';
    echo '<td><a href="' . $url . '">' . $url .'</a></td>' . PHP_EOL;
    echo '<td>' . $response . '</td>' . PHP_EOL;
    echo '</tr>';



    }
    fclose($handle);
    }else{
    echo 'cant open urls_to_check.csv';
    }
    echo '</tbody>';

    echo '</table>';

    echo '</body></html>';