file_get_contets redirect user to url Random

Asked

Viewed 274 times

0

<?php

$Arquivos = glob('artis/*.txt');
$ArquivoEscolhido = $Arquivos[rand(0,  count($Arquivos) - 1)];
$Linhas = file($ArquivoEscolhido);
$LinhaEscolhida = $Linhas[rand(0,  count($Linhas) - 1)];

$url = $LinhaEscolhida;

$url = str_replace(' ','',$url);

$vai = file_get_contents($url);

echo $vai;

?>

It searches the folder for a txt file (with several urls inside) in an Random shape and chooses a line inside this file and presents a url.

Within this url (.html) there are several other urls between:

<li class="ohyeah">

</li>

Details:

I have several txt files inside a folder

/pasta/1.txt
/pasta/2.txt 

Inside these files contains several urls

Exemplo:
/pasta/1.txt
conteúdo:
www.site.com/nome/jose.html
www.site.com/nome/joao.html


/pasta/2.txt
conteúdo:
www.site.com/nome/maria.html
www.site.com/nome/juliana.html

page contents . html

<li class="ohyeah">
<a href="http://www.site.com.br/nome/maria"> maria </a>
<a href="http://www.site.com.br/nome/joga"> joao </a>
(e várias outras)
</li>

In the code I can already find the url inside the files . txt form Random.

Now I need to access this url (within $url) to take the content of the page (chosen in Random form) and capture a single (multiple) url within:

<li class="ohyeah">

</li>
  • 2

    What’s the point of this? Why so many redirects?

  • Play everything for an array, and then do an array_rand

  • @Andersoncarloswoss, for a URL sharing system. The content of the site is not saved in a database so for me to capture the urls the way I need to... it’s the easiest way I could find.

  • @Everson, I don’t know enough. Can you help me?

  • I’ll edit the question to make it easier. A moment.

  • Put html content with urls here to help you. Edit your Question to keep cohesion !

  • @Rafaelsalomão, made!

  • Honestly, I still don’t understand. All the files .txt have a URL, and within each URL has a reference to a .html and in this HTML, they all have the tag <li class="ohyeah"> ?

  • @Everson, exact. I already get the URL inside the txt file. Now I need to search A single url inside the <li tag and redirect the user to it.

Show 4 more comments

2 answers

1

The simplest and most elegant way to do :

is to read this html with Doom:

<li class="ohyeah">
    <a href="http://www.site.com.br/nome/maria"> maria </a>
    <a href="http://www.site.com.br/nome/joga"> joao </a>
</li>

$dom = new DomDocument;
$dom->loadHTMLFile("https://url selecionada");
$xpath = new DomXPath($dom);
$nodes = $xpath->query("//li[@class='ohyeah']");
foreach ($nodes as $i => $node) {
   echo "Node($i): ", $node->nodeValue, "\n";
}

after this block and catch the urls with this regular expression of xpath then traversing the nodes with a loop you get the urls. It is evident that this code is incomplete the solution is hairier a little would take a while to write. But I gave you the whole concept.

1


On that line of your code $url = str_replace(' ','',$url); returns a Random url of one of the files txt of the directory pasta as an example www.site.com/nome/juliana.html

Right after you use file_get_contents; whose purpose is to read the contents of a file. Only you are trying to read the contents of a url file_get_contents($url); since $url returns for example www.site.com/nome/juliana.html and this will only generate an error and nothing more.

Warning:  file_get_contents(www.site.com/nome/juliana.html): 
failed to open stream: No such file or directory in .....

So remove that line from your code that serves nothing but generating one Warning.

If you already have a url to redirect, I don’t understand why the list of urls

<li class="ohyeah">
  .............
  .............
</li>

You should have your reasons there and therefore to do this you can use the code below.

$conteudoLista="";
$arr="";
$i=1;
$pasta = 'pasta/';
$diretorio = dir($pasta);
while($arquivo = $diretorio -> read()){
    if (file_exists($pasta.$i.".txt")) {
        $f = file($pasta.$i.".txt");
            foreach($f as $item){
               $arr .= $item .",";
               $strDir = str_replace(".html","",$item); 
               $nome = substr($strDir, 18);
               $conteudoLista .="<a href='http://".$item."'>".$nome."</a><br>"; 
               $conteudoLista = preg_replace( "/\r|\n/", "", $conteudoLista );
            }
    }
$i=$i+1;
}
$diretorio -> close();

$enderecos = (substr($arr,0,-1)); 
$partes = explode(',',$enderecos);
$qtd = count($partes);
$numRnad = (rand(1,$qtd));
$result=$partes[$numRnad];

echo "<li class=\"ohyeah\">".$conteudoLista."</li>";
echo "<br>";
echo "link random : ".$result;

Explaining:

$pasta = 'pasta/' - directory relative path

dir() - With this function, we will read the folder directory and, using the read() method, we will list all the existing files in this directory.

$diretorio = dir($pasta) - read the directory

while($arquivo = $diretorio -> read()) - iteration to go through the files by listing them with the read method()

if (file_exists($pasta.$i.".txt")) - make a check to know if the file exists

foreach($f as $item) - iteration in which $item receives the value of each array item

$arr .= $item ."," - concatenating each item separated by comma

str_replace - to remove (replace) the . html extension from the urls

substr($strDir, 18) - Extract a part of a string - returns the part of the url from position 18 to the end, i.e., eliminates - www.site.com/name/ - which is common to all urls

preg_replace( "/\r|\n/", "", $conteudoLista ) - performs a search for a regular expression and replaces it - n line break and r car return

substr($arr,0,-1) - removing the last $arr comma

$partes = explode(',',$enderecos)- Let’s separate the $addresses string in all comma occurrences

$qtd = count($partes)- get the number of elements that make up the $parts array

$numRnad = (rand(1,$qtd)) - generates a random number between 1 and the number of elements in the $parts array

$result=$partes[$numRnad] - returns the value of the $numRnad position of the $parts array

$diretorio -> close() - close the reading

Documentation used

Browser other questions tagged

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