Link Checking With php Script

Discussion in 'PHP' started by HellBomb, May 15, 2011.

  1. #1
    Ok, i honestly have no idea where to begin with this but I am trying to make a script that will go through a certain amount of URL's and look for one of them that is valid.

    Basicly I am trying to make a script that will look at the following URL.
    mywebsite.com/atn/1b7da100.htm
    see if it is a valid page, if it returns a 404 it will move to page
    mywebsite.com/atn/1b7da101.html
    So it will check through all pages 100-999 until it finds the right url then it will stop.
    mywebsite.com/atn/1b7da(changethisnumber).html

    Any suggestions on how i could accomplish this?
     
    HellBomb, May 15, 2011 IP
  2. mofta7y

    mofta7y Member

    Messages:
    29
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #2
    mostly you will need to use
    file_get_contents

    and check the return value if false or not
     
    mofta7y, May 15, 2011 IP
  3. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #3
    <?php
    for($i=100; $i<=999; $i++)
    {
    	$ch = curl_init();
    	
    	curl_setopt($ch, CURLOPT_URL, 'mywebsite.com/atn/1b7da' . $i . '.html');
    	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    		
    	$response  = curl_exec($ch);
    	$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    		
    	curl_close($ch);
    	
    	if($http_code != "404")
    	{
    		echo $response;
    		break;
    	}
    }
    PHP:
    Untested but should work. If a 404 isn't returned it'll output the contents of the page.
     
    crazyryan, May 15, 2011 IP
  4. m0nster

    m0nster Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    CURL would be ur best bet
     
    m0nster, Jun 2, 2011 IP