The Little Guy Posted January 30, 2012 Share Posted January 30, 2012 I am using this: $tags = @get_meta_tags($http["domain"]); Is there any way to tell if that function can not connect to the server for reasons such as server being down, or an invalid URL? Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted January 30, 2012 Share Posted January 30, 2012 is $http an array? otherwise you might try this $domain= $_POST["domain"]; $tags = @get_meta_tags("http://".$domain); Quote Link to comment Share on other sites More sharing options...
trq Posted January 30, 2012 Share Posted January 30, 2012 is $http an array? otherwise you might try this $domain= $_POST["domain"]; $tags = @get_meta_tags("http://",$domain); Besides your syntax being off I don't see what that has to do with the question. Op: There is no way of distinguishing why the function failed, it will just return an empty array. Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted January 30, 2012 Share Posted January 30, 2012 I am using this: $tags = @get_meta_tags($http["domain"]); Is there any way to tell if that function can not connect to the server for reasons such as server being down, or an invalid URL? by any chance, are you attempting to get meta tags from a secure site? (https) Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted January 30, 2012 Author Share Posted January 30, 2012 After deciding to do a var_dump, which I should have done before posting, it seems as if on a failed connection it returns false, and everything else it returns an array for. That should have been in the documentation. Another thing that would have been nice is if this supported an html string so I could use cURL on on it, but oh well. Quote Link to comment Share on other sites More sharing options...
Adam Posted January 30, 2012 Share Posted January 30, 2012 Another thing that would have been nice is if this supported an html string so I could use cURL on on it, but oh well. Not following you here? Surely if you know the URL to get the tags, you can construct a cURL request instead? Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted January 30, 2012 Author Share Posted January 30, 2012 Not following you here? Surely if you know the URL to get the tags, you can construct a cURL request instead? Currently all you can do is pass it a filename or a URL, it would be nice to be able to do this: // perform a curl request here, and get the html string back as $html_string // $html_string = "<html> // <head> // <title>Title</title> // <meta name="myMeta1" content="my content" /> // <meta name="myMeta2" content="my content 2" /> // <meta name="myMeta3" content="my content 3" /> // </head> // <!-- rest of html --> //</html>"; $tags = get_meta_tags($html_string); var_dump($tags); Quote Link to comment Share on other sites More sharing options...
Adam Posted January 30, 2012 Share Posted January 30, 2012 Oh I see. Well you realise using SimpleXML you could probably parse out that information pretty easily right? Even a simple regex would do the trick. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted January 30, 2012 Author Share Posted January 30, 2012 Oh I see. Well you realise using SimpleXML you could probably parse out that information pretty easily right? Even a simple regex would do the trick. SimpleXML doesn't work well on HTML files, especially if they have javascript/css written into the file. Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted January 30, 2012 Share Posted January 30, 2012 Even a simple regex would do the trick. Quote Link to comment Share on other sites More sharing options...
Adam Posted January 30, 2012 Share Posted January 30, 2012 Oh I see. Well you realise using SimpleXML you could probably parse out that information pretty easily right? Even a simple regex would do the trick. SimpleXML doesn't work well on HTML files, especially if they have javascript/css written into the file. Quite right. The DOM then: $html = '<!DOCTYPE html> <html> <head> <title>This is the title</title> <meta name="keywords" content="this is a self-closing tag" /> <meta name="description" content="this is not a self-closing tag"> <meta name=author content=idiot> <script> window.onload = function() { alert(\'here\\\'s some JavaScript..\'); } </script> </head> <body> </body> </html>'; $dom = new DOMDocument(); $dom->loadHTML($html); foreach ($dom->getElementsByTagName('meta') as $meta) { echo 'Meta ' . $meta->getAttribute('name') . ' = ' . $meta->getAttribute('content') . '<br />'; } Works even with some pretty badly written HTML. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.