Search code examples
phpmysqllaravelweb-scraping

How can I send scraped DATA to Mysql table with LARAVEL


I scraped some data with php and I want to send it to Mysql from Laravel. I connect to mysql but I don't know how to send it.

here is my scraped code;

$servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "laravel-test";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) 
    {

        die("Connection failed: " . $conn->connect_error);

    } 

    $curl = curl_init();
    curl_setopt($curl,CURLOPT_URL,"https://suumo.jp/ms/shinchiku/osaka/sa_osaka/");
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,1); 
    curl_setopt($curl,CURLOPT_FOLLOWLOCATION,1); 
    $sonuc = curl_exec($curl); 

    preg_match_all('@<div class="cassette-price">(.*?)</div>@si',$sonuc, $new);
    preg_match_all('@<ul class="cassette-plan">(.*?)</ul>@si',$sonuc, $info);
    preg_match_all('@<div class="cassette-header">(.*?)</div>@si',$sonuc, $name);


    $name = $name[0];
    $new = $new[0];
    $info = $info[0];


    for($i=0; $i < count($info);$i++)
    {
    echo $name[$i];
    echo $info[$i]; 
    echo $new[$i];  
    }

Thanks for help!!!

I am using laravel 5.7 by the way.


Solution

  • As far as I understand, you can just add the data to a regular table

    First, you can create a table (you've already done it, according to the comments)

    Then add configuration to Laravel [https://laravel.com/docs/5.7/database]

    The database configuration for your application is located at config/database.php. In this file you may define all of your database connections, as well as specify which connection should be used by default. Examples for most of the supported database systems are provided in this file.

    Then use Laravel database to add the data

    <?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Support\Facades\DB;
    use App\Http\Controllers\Controller;
    
    class UserController extends Controller
    {
        /**
         * Show a list of all of the application's users.
         *
         * @return Response
         */
        public function index()
        {
          for($i=0; $i < count($info);$i++)
          {
            DB::insert('insert into users (name, info, new)
            values (?, ?, ?)', [1, $name[$i], $info[$i], $new[$i] ]);
    

    You could also add data to an array, and insert it once :

    for($i=0; $i < count($info);$i++)
    {
               $data[] =[
                        'name' => $name[$i],
                        'info' => $info[$i],
                        'news' => $news[$i],
                       ];  
    }
    DB::insert($data);
    

    (see Insert Multiple Records At Once With Laravel )