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? Link to comment https://forums.phpfreaks.com/topic/256019-get_meta_tags-can-not-connect-to-server/ 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); Link to comment https://forums.phpfreaks.com/topic/256019-get_meta_tags-can-not-connect-to-server/#findComment-1312490 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. Link to comment https://forums.phpfreaks.com/topic/256019-get_meta_tags-can-not-connect-to-server/#findComment-1312493 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) Link to comment https://forums.phpfreaks.com/topic/256019-get_meta_tags-can-not-connect-to-server/#findComment-1312558 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. Link to comment https://forums.phpfreaks.com/topic/256019-get_meta_tags-can-not-connect-to-server/#findComment-1312611 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? Link to comment https://forums.phpfreaks.com/topic/256019-get_meta_tags-can-not-connect-to-server/#findComment-1312614 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); Link to comment https://forums.phpfreaks.com/topic/256019-get_meta_tags-can-not-connect-to-server/#findComment-1312628 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. Link to comment https://forums.phpfreaks.com/topic/256019-get_meta_tags-can-not-connect-to-server/#findComment-1312629 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. Link to comment https://forums.phpfreaks.com/topic/256019-get_meta_tags-can-not-connect-to-server/#findComment-1312663 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. Link to comment https://forums.phpfreaks.com/topic/256019-get_meta_tags-can-not-connect-to-server/#findComment-1312677 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. Link to comment https://forums.phpfreaks.com/topic/256019-get_meta_tags-can-not-connect-to-server/#findComment-1312681 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.