Problems with INSERT INTO in Crawler in PHP - Error passing variable as parameter

Asked

Viewed 28 times

0

<?php

require_once "./vendor/autoload.php";
use Goutte\Client;

$servername = "localhost";
$database  = "*******";
$username  = "root";
$password  = "********";

$conn = new mysqli($servername, $username, $password, $database);

if ($conn ->connect_error) 
{
  die("Falha na Conexão: " . $conn ->connect_error);
}

//echo "Conexão feita com sucesso. <br />";

$client = new Client();

$crawler = $client->request('GET', 'https://oneprovider.com/dedicated-servers/unmetered-bandwidth');

$crawler->filter('div[class="results-tr col-xs-12 col-sm-6 col-md-4 col-lg-12 no-padding"]')->each(function ($resultado, $conn) {  
    //print $resultado->text()."<br>";
    
    if(preg_match("/Canada/i", $resultado->text()))
    {
        //print $resultado->text()."<br>";

        if(preg_match("/Xeon /i", $resultado->text())) //W3520
            {
                //print $resultado->text()."<br>";

                    if(preg_match("/3.4/i", $resultado->text())) //2.9
                    {
                        //print $resultado->text()."<br>";

                            if(preg_match("/4/i", $resultado->text()))
                            {
                                //print $resultado->text()."<br>";

                                    if(preg_match("/32/i", $resultado->text()))
                                    {
                                        //print $resultado->text()."<br>";

                                            if(preg_match("/Unmetered/i", $resultado->text()))
                                            {
                                                print $resultado->text()."<br>";

                                                $sql_inserirDados = ("INSERT INTO espec_server (dc, cpu, core, thred, frequency, ram, bandwitch, region, currency, status)
                                                                       VALUES ('oneprovider', 'Intel Xeon W3520', 4, 8, '2.93', 32, 'unlimited', 'Canada', 'dolar', 'inactive')");
                                                $conn->query($sql_inserirDados);
                                            }
                                    }
                            }
                    }
            }
    }
});

$conn->close();

The error that occurs is:

Fatal error: Uncaught Error: Call to a Member Function query() on int in C: xampp htdocs Grunitzky Crawler teste.php:53 Stack trace: #0 C: xampp htdocs Grunitzky Crawler vendor symfony dom-Crawler Crawler.php(345): {closure}(Object(Symfony Component Domcrawler Crawler), 12) #1 C: xampp htdocs Grunitzky Crawler teste.php(60): Symfony Component Domcrawler Crawler->each(Object(Closure)) #2 {main} thrown in C: xampp htdocs Grunitzky Crawler teste.php on line 53

I tried to procedural the connection method along with the way to execute the SQL command, but it was not effective.

From what I understand, the problem is that the way I’m passing the variable $conn inside the function, it’s wrong. $conn receives the value of $resultado, i.e., the Crawler search array. Apparently it is necessary to find another way to pass $conn into the function.

1 answer

0

To pass variables into an anonymous function one must use the use as shown below.

$crawler->filter('div[class="results-tr col-xs-12 col-sm-6 col-md-4 col-lg-12 no-padding"]')->each(function ($resultado) use ($conn) { 

Browser other questions tagged

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