The Blog

Introducing Flatterizor

09 Mag 12

Flatterizor is a PHP library developed in order to issue a very specific problem.

Sometimes you have a complex object and you need to store it into your database. Most cases the serialize function does work. Unfortunately sometimes it does not ( not because serialize is broken/bugged ).

The Library is useful only if the followings are true:

  • You want to store your object inside a database
  • Your object is not “Flat”  ( meaning you’ve at least a value in your object that is an array or an object itself )
  • You want to use your Database SQL to Search & filter objects with a particular value.

The last sentence is actually the most appealing. Lets take, for example, the following objects:

Read More

Reading $_GET variable in CODEIGNITER

14 Ago 10

There are multiple solutions out there but i did create my own.

It’s simple as 1,2,3. Hope it helps.

[sourcecode lang=”php”]
// CODEIGNITER HACK
$tmp = explode(‘?’,$_SERVER[‘REQUEST_URI’]);
$tmp = explode(‘&’, $tmp[1]);

foreach($tmp as $keyval) {
$tmpAppoggio = explode(‘=’, $keyval);
$_GET[urldecode($tmpAppoggio[0])]=urldecode($tmpAppoggio[1]);
}
// end of codeigniter hack
[/sourcecode]

Usare Curl e Php per prendere il contenuto di una pagina web

20 Nov 08

Salve gente,
oggi voglio presentarvi una funzioncina che potrebbe tornarvi utile… Questa piccola guida è rivolta sopratutto ai programmatori php che si trovano spesso di fronte a problemi che per essere risolti utilizzano il web-scraping .

In Particolare, La mia funzione fa uso di php+curl per prendere il contenuto servito dalla pagina richiesta. L’uso della libreria curl permette di gestire molte più informazioni della semplice chiamata a fopen..

Inoltre curl, nei vari benchmark risulta essere ben 200 volte più veloce a serivire le richieste della normale chiamata a fopen(‘http….’);

Qui di seguito vi lascio il mio codice sorgente free.

Input: url della pagina da crawlare

Output: contenuto della pagina oppure null

Features: lo script ha un set limitato di user agent e li utilizza per simulare una vera chiamata a browser ( alcuni server potrebbero non servire affatto una risposta se il campo User-Agent non è settato )

[sourcecode lang=”php”]<?php
$someUA = array (
"Mozilla/5.0 (Windows; U; Windows NT 6.0; fr; rv:1.9.1b1) Gecko/20081007 Firefox/3.1b1",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.0",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/0.4.154.18 Safari/525.19",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13",
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)",
"Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.40607)",
"Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.1; .NET CLR 1.1.4322)",
"Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.1; .NET CLR 1.0.3705; Media Center PC 3.1; Alexa Toolbar; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
"Mozilla/45.0 (compatible; MSIE 6.0; Windows NT 5.1)",
"Mozilla/4.08 (compatible; MSIE 6.0; Windows NT 5.1)",
"Mozilla/4.01 (compatible; MSIE 6.0; Windows NT 5.1)");
function getRandomUserAgent ( ) {
srand((double)microtime()*1000000);
global $someUA;
return $someUA[rand(0,count($someUA)-1)];
}
function getContent ($url) {

// Crea la risorsa CURL
$ch = curl_init();

// Imposta l’URL e altre opzioni
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, getRandomUserAgent());
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
// Scarica l’URL e lo passa al browser
$output = curl_exec($ch);
$info = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Chiude la risorsa curl
curl_close($ch);
if ($output === false || $info != 200) {
$output = null
}
return $output;

}
?>[/sourcecode]

Per utilizzare questa semplice funzione basterà chiamare la funzione getContent con argomento l’url completo della pagina da ricevere.

Saluti, Andrea