Check if there is tag within site

Asked

Viewed 630 times

2

I generate a specific tag from my system, my customers need to add this tag within the bodyof their index, my problem is: How to identify if this tag is inside the site, only by its url, without informing the file name, for example:

Client informs its url: www.teste.com.br, and my system will access and check if the tag is there.

Currently I Gero one . html, the client uploads to your hosting and my system checks if a certain content exists in that uploaded file:

    $file_download = "1__".md5('1').".html";
    $url = "http://www.site.com.br/".$file_download.";

    $arquivo = $url ;
    $handle = @fopen($arquivo, "rb");
    $cont = @fread($handle, 100);

    if($cont == "verifica-licenca") {
        echo 1;
    } else {
        echo 0;
    } 

Working perfectly, but, I need the methodology of the tag, IE, the only problem is: Check if the content is in the main url, this, without informing the file

Vlw

  • That tag of yours is on body of the main page?

  • Yes, the tag should be inserted by the customer inside the main page body

  • What is the name of your tag?

  • If you can add details it would help. For example, how is the tag and how is present on a "real page".

3 answers

5

Another way would be using Domdocument from PHP, especially for those who don’t like REGEX. :P

Get the source code:

$ch = curl_init('http://seu_site_alvo.com');
curl_setopt_array($ch, [

    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
    CURLOPT_DNS_USE_GLOBAL_CACHE => true,
    CURLOPT_DNS_CACHE_TIMEOUT => 120,
    CURLOPT_SSL_VERIFYHOST => false,
    CURLOPT_SSL_VERIFYPEER => false

]);
$html = curl_exec($ch);
curl_close($ch);

You can use the file_get_contents or fopen also.

Then use the DOM:

// Inicia o DOM e XPath:
$DOM = new DOMDocument;
$DOM->loadHTML($html);
$XPath = new DomXPath($DOM);

// Obtem a contagem de todos os `<a>` que possuem o `HREF` de `http://www.site.com.br` e também que contenham o `CLASS` de `authority`.
if($XPath->evaluate("//a[contains(@class, 'authority') and @href='http://www.site.com.br']")->length >= 1){

        echo 'Encontrado';

}

That way you can know, without using REGEX, whether they contain HTML. The above code also makes this:

<a href="http://www.site.com.br" class="authority outra_coisa"></a>

Also be valid. If you do not want this, use @class='authority instead of contains(@class, 'authority'), for example.

  • The best way, if you change the order of the attributes, the letters are uppercase in the tag name, it will still work well +1

4


Good the code I believe is quite simple. First you need the source code of the site. that Voce can get as follows:

$codigofonte=file_get_contents("http://www.google.com");

With the source code in hand, it is simple to find your tag, I used a "regular expression" for in this case substituiryour tag, but I really just want to know if he replaced it, because then I know your tag exists.

$result=preg_match_all($tag,$codigofonte,$valorsubstituido);

On this line, the variable $result will have as value, true or false, that is, whether it found and replaced it or not.

Follows the code:

<?php

// pega o codigo fonte do site
$codigofonte=file_get_contents("http://www.google.com");

//sua tag
$tag='/<minhatag>/';

// 'busca' sua tag no coigo
$result=preg_match_all($tag,$codigofonte,$valorsubstituido);

if(!$result){
    echo "nao encontrado";
} else {
    echo "encontrado";
}


?>

1

Solved as follows:

                    // pega o codigo fonte do site
                $codigofonte=file_get_contents('http://www.google.com');


                //sua tag
                $tag='<a href="http://www.site.com.br" id="authority"></a>';
                    // 'busca' sua tag no codigo
                if (stripos(strtolower($codigofonte), $tag) !== false){
                        //Encontrado
                        echo 1;
                    }
                    else{
                        //Não Encontrado
                        echo 0;
                    }

Browser other questions tagged

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