Blog de programación, errores, soluciones

Chose Language:
Author: Admin/Publisher |finished | checked

Using file_get_contents() in PHP

The function file_get_contents in PHP is used to load a file into a string, and it returns false if it fails.

Description / Descripción
file_get_contents(
    string $filename,
    bool $use_include_path = false,
    resource $context = ?,
    int $offset = 0,
    int $maxlen = ?): string

In this section, rather than seeing how this function works, we will see what it is used for.

Suppose I want to crawl someone else’s webpage, in that case, I may want to obtain that webpage in the form of a string.

Once I have obtained the HTML of the webpage in string form, I can see where the links are and go through them one by one.

Let’s try to obtain the HTML of Google.com. It is possible that some pages do not allow this. This is called a crawler, a small robot that obtains information from other pages, in fact, Google has its own and that is how it indexes our page. Of course, I have no idea what language Google uses, probably it’s made in C or C++.

obteniendo el contenido de google.com
<?php

$p = file_get_contents('http://google.com/');
if($p !==false){
    echo "<textarea>$p</textarea>";
}else{
    echo "no pudo obtenerse el contenido de la pagina";
}
?>

echo will attempt to display the Google page on your page.

It’s possible that you may want to make a crawler to copy another site and create a site quickly. Don’t do it, Google knows that your website is a copy of another and will put that other site ahead of the one you create. This will result in a penalty, pushing your site even further down the rankings. It won’t rank well either.

For obvious reasons, this post does not teach how to make a crawler, as it would be too long to explain everything. And there are still more things we can do with file_get_contents.

Using a JSON file with file_get_contents() in PHP

https://blastcoding.com/en/using-file_get_contents-in-php/#json

We can also use a JSON file, using file_gets_contents and json_decode. If we have a JSON of menus like the following:

menu.json
{"menues": [
    {
        "id": 1,
        "title": "Hamburguesas",
        "image": "hamburguesas.jpg"
    },
    {
        "id": 5,
        "title": "Porteño",
        "image": "portenos.jpg"
    }]
}

Now we will open and obtain the content of this file, let’s see how to do it:

ejemplo2.php misma carpeta de menu.json
$a =file_get_contents("menu.json");
if($a !==false){
    $a=json_decode($a,true);
    var_dump($a['menues']);
}else{
    echo "no pudo obtenerse el contenidod el archivo JSON";
}
?>

Probably you’re thinking, but you’re not iterating anything, that’s just putting a foreach in short words we delete that var_dump and put a foreach.

ejemplo2.php
foreach ($a['menues'] as $menu){
        echo "<p>".$menu['id']."<p>";
        echo "<p>".$menu['title']."<p>";
        echo "<p>".$menu['image']."<p>";
    }
Category: en-php
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category