Calcetines Extreme

Calcetines Extreme
Take care of you using the best socks

Tuesday, October 27, 2020

How to scrap elements from external website

Problem:

You need to pic information from a public website and process it internally to do some staff.

Solution:

You can use PHP Simple HTML DOM parse as the easy way, follow the next steps:

  1. Download the sources from the oficial web site
  2. copy all files into your web server
  3. create a new file example.php
  4. use this code:

include_once('simple_html_dom.php');

$context = stream_context_create();

stream_context_set_params($context, array(

            'ignore_errors' => true, 

            'max_redirects' => 3)

            );

$html = file_get_html('https://www.elmundo.es', 0, $context);

$articles_titles = $html->find(".ue-c-cover-content__link");

foreach($articles_titles as $article_title) {

    echo "<BR>\t\t".$article_title->plaintext."\n";

}

$html->clear();

unset($html);

echo "<BR>";echo "<BR>";echo "<BR>";

echo "<BR>OK";

Information That's it, now you only need to analyse the information that you get as html and process it in your function or program, take care about your objective, the server that you use to extract the information can ban your IP; to avoid that it's possible to use a proxy as is indicated int he source link.

Source:

https://www.fernandezsansalvador.es/hacer-web-scraping-con-php-simple-html-dom-parser/


Monday, October 19, 2020

How to setup PHP mvc minimun model without any framework

 Problem:

You need to create a minimum web site or web app using php model but it's preferred to use any framework to avoid load the system without any unnecessary code.

Solution:

Create the following folder and fields structure in the server.
\
\\controllers\sample_controler.php
\\db\connection.php
\\models\sample_model.php
\\views\sample_view.html
\\index.php

Use the next code in each file:

\\controllers\sample_controler.php
<?php
require_once("models/sample_model.php");
$samp=new sample_model();
$data=$samp->get_samplequery();
require_once("views/sample_view.html");
?>
\\db\connection.php
<?php
class Connect{
    public static function connection(){
        $connection=new mysqli("localhost", "root", "pwd", "mvc");
        $connection->query("SET * FROM table 'UTF8' ");
        return $connection;
    }
}
?>

\\models\sample_model.php
<?php
class sample_model{
    private $DB;
    private $sample;
    public function __construct(){
        $this->DB=Connect::connection();
        $this->sample=array();
    }
    public function get_samplequery(){
        $query=$this->db->query("select * from table;");
         ...
        return $this->query;
    }
}
?>

\\views\sample_view.html
<html lang="es">
    <head>
        <meta charset="UTF8" />
        <title>Sample</title>
    </head>
        <?php echo "put your database query here"        ?>
</html>

\index.php
<?php
require_once("db/connection.php");
require_once("controllers/sample_controler.php");
?>