get_meta_tags does not work

Asked

Viewed 182 times

1

I made a script in Javascript and PHP, in which I need to get the first keyword (meta keyword) page document.referrer.

No use using the $_SERVER["HTTP_REFERER"] which does not work in my case, because the page being executed is inside a iframe, that way, it doesn’t 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 to trigger the get_meta_tags, if I give a echo in the variable, it shows the right URL, but does not work at the time of picking the tags, I do not know if it is the type of variable that has to change, or something by style.

I tested switching to $referenciaphp = "http://www.uol.com.br/";, and works perfectly the script.

The problem is only when using a variable coming from Javascript, I don’t know if you have to convert the variable or something like.

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

1 answer

1

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

There are other ways to do this even better (I won’t go into the other problems of your code) but the closest you are 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>

I put in the Github for future reference.

This code will execute when there is a click on the name button 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 facilitate. Others prefer it this way, the Vanilla JS.

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

In PHP suapagina.php you will receive the variable thus:

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

I put in the Github for future reference.

AJAX reference on MDN.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.