PHP - PHP Grabber

 
Vista:

PHP Grabber

Publicado por Cesar (1 intervención) el 03/04/2012 06:53:08
Hola.
Necesito ayuda!
Verán, tengo este código. Lo que quiero hacer es mandar esos array a una base de datos. Pero... cómo?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
 
//Busqueda
$imdb = new Imdb();
$movieArray = $imdb->getMovieInfoById($_POST["num"]);
echo '<table id="tabla" cellpadding="3" cellspacing="2" border="1" width="80%" align="center">';
foreach ($movieArray as $key=>$value){
    $value = is_array($value)?implode("<br />", $value):$value;
    echo '<tr>';
    echo '<th align="left" valign="top">' . strtoupper($key) . '</th><td>' . $value . '</td>';
    echo '</tr>';
    echo '</table>';
}
 
 
//Obtener datos
 
class Imdb
{
	function getMovieInfoById($imdbId)
    {
        $arr = array();
        $imdbUrl = "http://www.imdb.com/title/" . trim($imdbId) . "/";
        $html = $this->geturl($imdbUrl);
        if(stripos($html, "<meta name=\"application-name\" content=\"IMDb\" />") !== false){
            $arr = $this->scrapMovieInfo($html);
            $arr['imdb_url'] = $imdbUrl;
        } else {
            $arr['error'] = "¡No se econtro en IMDb!";
        }
        return $arr;
    }
 
     function scrapMovieInfo($html)
    {
 
$arr = array();
 
$arr['titulo'] = trim($this->match('/<title>(IMDb \- )*(.*?) \(.*?<\/title>/ms', $html, 2));
 
$arr['year'] = trim($this->match('/<title>.*?\(.*?(\d{4}).*?\).*?<\/title>/ms', $html, 1));
 
$arr['rating'] = $this->match('/ratingValue">(\d.\d)</ms', $html, 1);
 
$arr['generos'] = array();
        foreach($this->match_all('/<a.*?>(.*?)<\/a>/ms', $this->match('/Genre.?:(.*?)(<\/div>|See more)/ms', $html, 1), 1) as $m)
            array_push($arr['generos'], $m);
 
$arr['director'] = array();
        foreach($this->match_all('/<a.*?>(.*?)<\/a>/ms', $this->match('/Director.?:(.*?)(<\/div>|>.?and )/ms', $html, 1), 1) as $m)
            array_push($arr['director'], $m);
 
$arr['cast'] = array();
        foreach($this->match_all('/<td class="name">(.*?)<\/td>/ms', $html, 1) as $m)
            array_push($arr['cast'], trim(strip_tags($m)));
 
$arr['sinopsis'] = trim(strip_tags($this->match('/<p itemprop="description">(.*?)(<\/p>|<a)/ms', $html, 1)));
 
$arr['duracion'] = trim($this->match('/Runtime:<\/h4>.*?(\d+) min.*?<\/div>/ms', $html, 1));
        if($arr['duracion'] == '') $arr['duracion'] = trim($this->match('/infobar.*?(\d+) min.*?<\/div>/ms', $html, 1));
 
$arr['idioma'] = array();
        foreach($this->match_all('/<a.*?>(.*?)<\/a>/ms', $this->match('/Language.?:(.*?)(<\/div>|>.?and )/ms', $html, 1), 1) as $m)
            array_push($arr['idioma'], trim($m));
 
$arr['pais'] = array();
        foreach($this->match_all('/<a.*?>(.*?)<\/a>/ms', $this->match('/Country:(.*?)(<\/div>|>.?and )/ms', $html, 1), 1) as $c)
            array_push($arr['pais'], $c);
        return $arr;
 
 
    }
 
 
 
// ************************[ Extra Functions ]******************************
    function geturl($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        $ip=rand(0,255).'.'.rand(0,255).'.'.rand(0,255).'.'.rand(0,255);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("REMOTE_ADDR: $ip", "HTTP_X_FORWARDED_FOR: $ip"));
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/".rand(3,5).".".rand(0,3)." (Windows NT ".rand(3,5).".".rand(0,2)."; rv:2.0.1) Gecko/20100101 Firefox/".rand(3,5).".0.1");
        $html = curl_exec($ch);
        curl_close($ch);
        return $html;
    }
 
    function match_all($regex, $str, $i = 0)
    {
        if(preg_match_all($regex, $str, $matches) === false)
            return false;
        else
            return $matches[$i];
    }
 
    function match($regex, $str, $i = 0)
    {
        if(preg_match($regex, $str, $match) == 1)
            return $match[$i];
        else
            return false;
    }
 
}
 
echo '<body>';
echo '<form method="post" action=".php" />';
echo '<input type="submit" values="TEST" name="boton">';
echo '</body>';
?>

El botón al final es para que al darle clic me agregue los difrerentes $arr a la base de datos.

1
2
$insert ="INSERT INTO personas(Titulo,Año,Rating) VALUES ("$arr['titulo']","$arr['year']","$arr['rating']")";
	mysql_query($insert) OR die(mysql_error());

Es esta la forma correcta?

Hay alguna otra manera de que mis datos en los array vayan a mi base de datos?
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder