file_get_contets redirect user to url random

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) randomly and chooses a line inside this file and displays 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 

Within 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 content .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 get the url inside the .txt files in a random way.

Now I need to access this url (within $ url) to grab the contents of the page (chosen randomly) and capture a single url (of several) within:

<li class="ohyeah">

</li>
    
asked by anonymous 04.07.2017 / 20:57

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";
}

Then filter out this block and get the urls with this regular expression from xpath then traversing the nodes with a loop you get the urls. Obviously this code is incomplete, the solution is more hairy a little bit it would take a while to write. But I gave you the whole concept.

    
04.07.2017 / 21:25
0

In this line of your code $url = str_replace(' ','',$url); returns a random url from one of the txt files in the pasta directory, such as www.site.com/nome/juliana.html

Soon after you use file_get_contents; whose purpose is to read the contents of a file. But you're 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 this line from your code that is good for nothing but generate Warning .

If you already have a random url to do the redirect, I did not 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/' - relative directory path

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

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

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

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

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

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

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

substr($strDir, 18) - Extract a part of a string - returns the part of the url from position 18 to the end, that is, delete - www.site.com/nome/ - 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 carriage return

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

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

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

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

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

$diretorio -> close() - closed reading

Documentation used

05.07.2017 / 03:35