get_meta_tags does not work

1

I made a script in JavaScript and PHP, in which I need to get the first keyword

It does not help to use document.referrer which does not work in my case, because the page you are running is within an iframe , so it does not work.

So I made the following code:

 
// O script abaixo, pega a URL presente do navegador, até aqui funciona perfeito, ele pega certinho o que preciso
<script>
function getParentUrl() {
    var isInIframe = (parent !== window),
        parentUrl = null;

    if (isInIframe) {
        parentUrl = document.referrer;
    }

    return parentUrl;
}
var referencia = getParentUrl();
</script>
<?php
// a variavel abaixo, pega o valor do javascript, e recebe certinho a variavel
$referenciaphp = "<script>document.write(referencia)</script>";
echo $referenciaphp;

//abaixo está o problema
$tags = get_meta_tags($referenciaphp);

echo $tags;
$palavras=$tags['keywords'];
$individual=explode(",", $palavras);
echo $individual[0];
?>

The problem is when I trigger $_SERVER["HTTP_REFERER"] , if I give get_meta_tags to the variable, it shows the correct URL, but it does not work when I get the tags, I do not know if it's the type of variable that have to change, or something by style.

I tested switching to echo , and the script works perfectly.

The problem is only when using a variable coming from JavaScript, I do not know if I have to convert the variable or something.

    
asked by anonymous 07.02.2015 / 15:09

1 answer

2

Although you are seeing the variable in the same file, when you run the PHP part and the JavaScript part are not together, PHP runs on the server and JS runs in the browser. They do not talk directly, their code needs to provide a form of communication between them.

There are other ways to do this even better (I will not go into the other problems of your code) but the closest thing you're doing is sending the variable to PHP via AJAX.

<script>
function getParentUrl() {
    var isInIframe = (parent !== window),
        parentUrl = null;

    if (isInIframe) {
        parentUrl = document.referrer;
    }

    return parentUrl;
}
var referencia = getParentUrl();

(function() {
  var httpRequest;
  document.getElementById("ajaxButton").onclick = function() {
          makeRequest('suapagina.php?referencia=' + referencia); };

  function makeRequest(url) {
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
      httpRequest = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
      try {
        httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
      } 
      catch (e) {
        try {
          httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } 
        catch (e) {}
      }
    }

    if (!httpRequest) {
      alert('Giving up :( Cannot create an XMLHTTP instance');
      return false;
    }
    httpRequest.onreadystatechange = alertContents;
    httpRequest.open('GET', url);
    httpRequest.send();
  }

  function alertContents() {
    if (httpRequest.readyState === 4) {
      if (httpRequest.status === 200) {
        alert(httpRequest.responseText);
      } else {
        alert('There was a problem with the request.');
      }
    }
  }
})();
</script>

This code will execute when there is click on the button named ajaxButton . But it is possible to change to occur on page load:

window.onload = function() { makeRequest('suapagina.php?referencia=' + referencia); };

Some people prefer to use jQuery to make it easier. Others prefer this way, the Vanilla JS .

And note that this code is a basic recipe, it was not created for your specific needs, it needs to be adapted.

In PHP suapagina.php you will receive the variable like this:

<?php
$referenciaphp = $GET["referecia"];
$tags = get_meta_tags($referenciaphp);
$palavras=$tags['keywords'];
$individual=explode(",", $palavras);
echo $individual[0];
?>

MDN AJAX Reference .

    
07.02.2015 / 16:26