Get metadata from a url

2

Currently I need to obtain the metadata with a given URL from javascript, but I always have the CORS problem.

With the following code I can read the metadata of the own page but I am interested in being able to obtain the information from an external page.

<script>
function metaDatos() {
    var x = document.getElementsByTagName("META");
    var txt = "";
    var i;
    for (i = 0; i < x.length; i++) {
        txt = txt + "Content of "+(i+1)+". meta tag: "+x[i].content+"<br>";
    }

    document.getElementById("demo").innerHTML = txt;
}
</script>

Greetings! To see if someone can help me with the subject of the requests ..:)

    
asked by Kiko 12.10.2016 в 22:52
source

1 answer

1

You should use a PHP file that reads the page you want to scan and so to avoid CORS:

<?php

/* proxy.php */

header('Content-Type: text/plain');
header("Access-Control-Allow-Origin: *");
$url = $_GET['url'];
$ch  = curl_init($url);
curl_exec($ch);
curl_close($ch);

To that PHP you make your Ajax call passing as a parameter the url you want:

<script>
    function getMetaDatos(url) {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', 'proxy.php?url='+url);
        xhr.send(null);
        xhr.onreadystatechange = function () {
            var DONE = 4;
            var OK = 200;
            if (xhr.readyState === DONE) {
                if (xhr.status === OK) {
                    console.log(xhr.responseText);
                    var html = document.createElement('html');
                    html.innerHTML = xhr.responseText;
                    var x = html.getElementsByTagName('meta');
                    var txt = "";
                    var i;
                    for (i = 0; i < x.length; i++) {
                        txt = txt + "Content of "+(i+1)+". meta tag: "+x[i].content+"<br>";
                    }
                    document.getElementById('demo').innerHTML = txt;
                } else {
                    console.log('Error: ' + xhr.status);
                }
            }
        };
    }
    getMetaDatos('http://www.google.es');
</script>
    
answered by 13.10.2016 в 18:42