Create a php code to broken link?

Discussion in 'PHP' started by fixesign, Apr 22, 2011.

  1. #1
    I have a code like this:
    $myURL = 'http://www.yahoo.com';
    preg_match('/<title>(.+)<\/title>/',file_get_contents($myURL),$matches);
    $title = $matches[1];
    echo"$title";
    PHP:
    But if I put the link wrong / broken, I see the error:
    Warning: file_get_contents () [function.file-get-contents]: php_network_getaddresses: getaddrinfo failed: No such host is known...
    How to make php code to validate the url? thanks in advance.
     
    fixesign, Apr 22, 2011 IP
  2. fixesign

    fixesign Member

    Messages:
    175
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #2
    What I mean here is that when I enter the url in the text box, the url will be checked whether actually found. If not found, I want to make the error message "Your link was not found!". But if the url can be explored, I want to get the title of the site. What friends can help me make the code to check the URL?
     
    fixesign, Apr 23, 2011 IP
  3. ap2010

    ap2010 Guest

    Messages:
    41
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    $result = file_get_contents("http://someurl/somepage");
    if($result === false) {
        // The function returns the read data or FALSE on failure
        echo "An error occurred while attempting to fetch the page!";
        $error = error_get_last();
        var_dump($error);
    }
    
    PHP:
    Edit

    file_get_contents("http://") is a quick and dirty way to fetch http data. If you are interested in getting more information and/or verbose details such as raw http headers, http status codes, error details like dns lookup failure etc, you should use the fsockopen functions, or better, use CURL library.
     
    Last edited: Apr 23, 2011
    ap2010, Apr 23, 2011 IP
  4. fixesign

    fixesign Member

    Messages:
    175
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #4
    If so, how do I use CURL library to obtain the title of the website? thanks in advance,friends.
     
    fixesign, Apr 23, 2011 IP
  5. rajeev_seo

    rajeev_seo Peon

    Messages:
    211
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    when
    l you want to know for curl library use see php.net for more information
    its the best site for its all functionality and use of curl
     
    rajeev_seo, Apr 23, 2011 IP