I recently developed some code that allowes me to get the title of a URL posted into a form, it works for most websites, other's it doesn't, and for one that I came across in beta testing outputs this error
Yes I know it's a strange thing to be typing it, but I was testing me blocking filters as websites like these arn't allowed on the website, but it outputed this error, which is interesting
Warning: file_get_contents((snip)) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in /home/hrefdir/public_html/submit.php on line 71
This seems to be the only URL i've tried so far which it happens with, the code I am using is...
function getTitle($link){
$str = file_get_contents($link);
if(strlen($str)>0){
preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);
return $title[1];
}
}<?php
$gettitle = getTitle($url);
if($gettitle == ""){
?>
<input type="text" name="title" class="submitform" value="Website Title" onFocus="if(this.value == 'Website Title') {this.value = '';}" onBlur="if (this.value == '') {this.value = 'Website Title';}" />
<?php
}else{
?>
<input type="text" name="title" class="submitform" value="<?php echo getTitle($url); ?>" />
<?php
}
?>
And the URL to the site I'm using it on, The Hyperlink Directory - The ultimate web directory and backlink creatorTry entering a URL, and you will see the title of the page come up automatically.
Any help on this would be appreciated.
Also, when I enter a URL that doesn't exist I get these errors
Warning: file_get_contents() [function.file-get-contents]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/hrefdir/public_html/submit.php on line 71
Warning: file_get_contents(http://oswgvgdewgewegcall.net) [function.file-get-contents]: failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/hrefdir/public_html/submit.php on line 71
Any idea on how I can change this?
Edited by Alexander, 05 April 2011 - 06:57 PM.
(Removed unintentional hyperlinking to porn site)


Sign In
Create Account


Back to top









