Help to transform a for each ( from a php file_get_contents ) into Json Encode

Asked

Viewed 88 times

0

to performing a file_get_contents php on a site, ai shows all teams of this site, in the for each loop it shows me all the time ( that are between the file tags ), but wanted to turn all these teams into Json Find ( turn into API ).

Because of file_get_contents php I can not transform this data into array to be displayed in Json Encode, I tried a lot and did not have a positive result, I will leave my code, if someone gets this.

only thing I got was a response ( a team ), in case I wanted all teams.

<?php
echo "[";
// URL DO SITE
$url = ' url do site';

// PEGANDO TODO CONTEUDO
$dadosSite = file_get_contents($url);

$var1 = explode('<h4><b>',$dadosSite);

//possivel filtro


foreach( $var1 as $idArray => $array){

if($i)
{

 $horajogo = explode('</b></h4>',$array);

//=======> time um
  // filtrar o que voce quer 
  $timeum = explode('width="25" height="25">',$array);
  // filtrar ate onde voce quer
  $timeum = explode('</div>',$timeum[1]);
 
  
$cliente1 = array(
    'codigo'   => $timeum[0],
);
}
 
$i++;
}



echo json_encode($cliente1, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);



?>

I wanted to do something like, but it shows up ALL the teams.

[{
    "camp": "Campeonato Brasileiro",
    "hrs": "22:00",
    "time": "Arsenal",
    "img": "https://futebolnatv.com.br/static/times/33a929f9e1e26c8a52e0b2c1bee44d6e.png",
    "gol": "1",
    "goldois": "5",
    "timedois": "Liverpool",
    "imgdois": "https://futebolnatv.com.br/static/times/abe768c15fc134e24d933eef88e5d438.png",
    "tempo": "Ao vivo PENALTIS",
    "trans": "SPORTV(-RS), PREMIERE FC"
}]

1 answer

0


In the example below I am using Domxpath to navigate, assuming, from what I saw of the HTML content, that only one table exists on this page.

You need to improve according to details of your need, but in this example I am taking each column of each row and generating an entry in an array row (the time and the game).

See that the question you raised has nothing to do with the way you are reading the data, using file_get_contents, but rather with how to parse this data source.

<?php
$url = 'https://futebolnatv.com.br/jogos-hoje/';

// PEGANDO TODO CONTEUDO
$html = file_get_contents($url);

$doc = new DOMDocument();
$doc->loadHTML($html);

libxml_clear_errors(); //remove erros de html quebrado

$xpath = new DOMXPath($doc);
$table = $xpath->query('//table')->item(0);
$rows = $table->getElementsByTagName("tr");

$dados=[];
foreach($rows as $row)
  {
    $linha = [];
    $jogo = $row->getElementsByTagName('td');
    $hora = $row->getElementsByTagName('th');
    $linha['hora'] = $hora[0]->nodeValue;
    $linha['jogo'] = $jogo[0]->nodeValue;
        
    $dados[] = $linha;
  }

  print_r(json_encode($dados));
  • really the way it worked, I will look for information on the internet to understand about this Domxpath, Thank you very much

  • That’s great, as it is new in the community I will guide how to proceed now, if you can, mark my answer as accepted, so my score increases and allows me to have access to more stackoverflow tools to contribute to the platform

  • Okay, can you give me some video on this case ? Thank you

  • Thank you. As for the video, unfortunately I don’t know any, but I’m creating content training on my channel, I can note it as content pending on DOM with PHP but I can’t guarantee you that I would do it this week, most likely in about 15 days. Follow me on Youtube that gets the video warning when available

  • 1

    Mazer dev ? followed your channel

  • Yes, but I think the Youtube search finds me by my name Ademir Mazer Junior

Show 1 more comment

Browser other questions tagged

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