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.
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?
$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.
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