CRAWLER DE SITE EN PHP

cs_rosamoon Messages postés 2 Date d'inscription vendredi 29 octobre 2004 Statut Membre Dernière intervention 30 novembre 2009 - 30 nov. 2009 à 01:21
cs_Mcjo Messages postés 403 Date d'inscription dimanche 12 août 2001 Statut Membre Dernière intervention 3 septembre 2012 - 3 déc. 2009 à 15:17
Cette discussion concerne un article du site. Pour la consulter dans son contexte d'origine, cliquez sur le lien ci-dessous.

https://codes-sources.commentcamarche.net/source/50900-crawler-de-site-en-php

cs_Mcjo Messages postés 403 Date d'inscription dimanche 12 août 2001 Statut Membre Dernière intervention 3 septembre 2012 2
3 déc. 2009 à 15:17
bon je te renvoie un code source ou j'ai remplacé la constante par une variable et définie des fonctions sans public (mais ça ne change rien puisque en php si pas private -> public)

Maintenant si tu as un problème vérifie que tu sois en php et surtout que tu ais le module CURL pour php

:

<?php
/**
* Crawler
* botcrawler
*
* @package Crawler
* @since 1.0
* @version $Revision: 1 $
*/
class Crawler {

var $USER_AGENT = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14';
var $authentification_form;
var $authentification_url;
var $must_auth;
var $site_name;
var $cookies_path;
var $user_agent;

/**
* Constructor
*
* @param string $site_name name of site used for define the name of the cookie
* @param boolean $must_auth define if the site requiere an authenficiation - false by default,
* @param string $authentification_url url for authentification - empty by default
* @param array $authentification_form mixed value of the authenfication form
* @param string $user_agent set another user_agent
* @return void
*
*/function Crawler($site_name, $must_auth false, $authentification_url "", $authentification_form = array(), $user_agent = null, $debug = false) {

$this->user_agent = (!is_null($user_agent)?$user_agent:$this->USER_AGENT);

$this->site_name = $site_name;
$this->must_auth = $must_auth;
$this->authentification_url = $authentification_url;
$this->authentification_form = $authentification_form;
if (is_file("cookies/{$this->site_name}_cookie.txt")) {
unlink("cookies/{$this->site_name}_cookie.txt");
}
$fp_cookies = fopen("cookies/{$this->site_name}_cookie.txt", "w+");
fwrite($fp_cookies, '');
fclose($fp_cookies);

$this->cookies_path = realpath("cookies/{$this->site_name}_cookie.txt");

if ($this->must_auth) {
$ch = curl_init($authentification_url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $authentification_form);
curl_setopt($ch, CURLOPT_COOKIEFILE , $this->cookies_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookies_path);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_COOKIESESSION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
$ret = curl_exec($ch);
if ($ret === FALSE) {
die(curl_error());
}
if ($debug) echo "Debug :\n".print_r($ret, true)."

";
curl_close($ch);
if (preg_match('/(PHPSESSID=[0-9a-z,-]{32,40})/i', $ret, $m)) {
$sid = '?' . $m[1];
} else if (preg_match('#\'"\\\]+)"\s+value="([0-9a-z,-]{32,40})"\s*/?>#i', $ret, $m)) {
$sid = '?' . $m[1] . '=' . $m[2];
}
}
}

/**
* method to access to a page by get
*
* @param string $url
* @param integer $timeout
* @return content of page
*/
function http_fetch_url($url, $timeout = 1000)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_COOKIEFILE , $this->cookies_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookies_path);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_COOKIESESSION, false);
curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
$data = curl_exec($ch);
curl_close($ch);

return $data;
}

/**
* method to access to a page by post
*
* @param stirng $url
* @param array $form array of string value of data post
* @param integer $timeout
* @return content of page
*/
function http_post_url($url, $form = array('none'=>'none'), $timeout = 1000)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_COOKIEFILE , $this->cookies_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookies_path);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($form));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_COOKIESESSION, false);
curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}

}

?>
tagalog75 Messages postés 2 Date d'inscription jeudi 15 mars 2007 Statut Membre Dernière intervention 2 décembre 2009
2 déc. 2009 à 22:33
Ca marche pas plus, désolé.
Comme la classe crawler du php5 n'est pas installée sur les 2 serveurs restés en php4 je sais plus combien, il faut adapter et mettre des //devant public par exemple.

Les fonctions fetch et get de récupération des données n'étant pas reconnues, la seule possibilité est de les enlever, le code fonctionne.
Oui, mais voilà : ça passe sans erreur mais comme on ne récupère aucune donnée d'aucun site, on affiche une page blanche.
Il faut juste trouver le moyen de récupérer les données dans le fichier sample.php.
Une idée peut-être ?
merci
cs_Mcjo Messages postés 403 Date d'inscription dimanche 12 août 2001 Statut Membre Dernière intervention 3 septembre 2012 2
30 nov. 2009 à 14:59
Tu es sure d'être en php 5.1.2 minimum ?
Il semble que ta version de php ne gère pas les constante de classe.
Remplace
const USER_AGENT ...
//const USER_AGENT

et la ligne

$this->user_agent = (!is_null($user_agent)?$user_agent:Crawler::USER_AGENT);

par

$this->user_agent = (!is_null($user_agent)?$user_agent:'Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14');
tagalog75 Messages postés 2 Date d'inscription jeudi 15 mars 2007 Statut Membre Dernière intervention 2 décembre 2009
30 nov. 2009 à 11:48
Pas que che Free : même erreur :
Idem pour mon compte professionnel chez Amen :
Parse error: parse error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /home/.sites/69/siteX/web/monespace/crawler/classes/crawler.php on line 12

Alors, que faire ? si vous avez une solution, je suis preneur.
cs_Mcjo Messages postés 403 Date d'inscription dimanche 12 août 2001 Statut Membre Dernière intervention 3 septembre 2012 2
30 nov. 2009 à 09:49
Mais de toute façon Free n'autorise pas l'ouverture de socket depuis php (à moins que cela est changé), donc ça risque de ne pas marcher.
cs_Mcjo Messages postés 403 Date d'inscription dimanche 12 août 2001 Statut Membre Dernière intervention 3 septembre 2012 2
30 nov. 2009 à 09:47
Ça veut dire que sur free, vous devez activer php5, en plaçant le .htaccess adéquate.
Bonne journée
cs_rosamoon Messages postés 2 Date d'inscription vendredi 29 octobre 2004 Statut Membre Dernière intervention 30 novembre 2009
30 nov. 2009 à 01:21
Parse error: syntax error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /mnt/123/free.fr/1/7/mon_compte/crawler/classes/crawler.php on line 12

ça veut dire quoi ? impossible chez free ?
Rejoignez-nous