Jump to content

get_meta_tags(): Can Not Connect to Server


The Little Guy

Recommended Posts

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.

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)

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.

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?

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);

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.