Fetch data from an external page with file_get_contents

Asked

Viewed 352 times

-1

I’m using the remote file_get_contents because I want to take information from a website and paste that information into my page.

Can someone help me implement the code so I can get this information?

// Endereço do site
$url = 'https://api.micetigri.fr/player/Leow%239880';

// Pegando dados do Site e colocando em uma String
$dadosSite = file_get_contents($url);

// Exibindo o retorno
echo $dadosSite;

Well he does print, only I do not know how to get the information I want below:

inserir a descrição da imagem aqui

I just wanted to play this result on my website.

1 answer

1


You can capture this information using the function preg_match(), the code would look like this:

$url = "https://api.micetigri.fr/player/Leow%239880";
$contents = file_get_contents($url);
(bool) $operacao = preg_match("/<span id=\"ip_cliente\">(.*)<\/span>/", $contents, $dadosRetornados);

if($operacao)
    echo $dadosRetornados[1];
else
    echo "Falha na operação";

The first parameter of preg_match() is the regular expression to be executed, as you may have noticed, I used a <span> fictitious for simulation, note the html where this value is returned and replace.

  • Good friend, thank you for your strength! your code helped in parts, like I have another question.How do I implode a complete table in this code ex: <table class="table table-bordered table-Hover table-Striped"> FULL TABLE</table>

  • This can vary a lot according to the table structure, but for an overall solution, you could make a loop. I created an array with all the id’s of the values you want to capture and store the data acquired with preg_match() in another array at each loop iteration.

  • I’ll try here buddy, thanks for the help!

  • I almost forget, there is also the function preg_match_all() that can be used for you, take a look http://php.net/manual/en/function.preg-match-all.php, it will return all values that contain the expression.

Browser other questions tagged

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