Skip to content

Instantly share code, notes, and snippets.

@msaby
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msaby/f2ca3279caef7d3eb756 to your computer and use it in GitHub Desktop.
Save msaby/f2ca3279caef7d3eb756 to your computer and use it in GitHub Desktop.
Récupération des profils de poste dans Poppee
<?php
# Récupération des fiches de postes au mouvement de conservateur d'après le site Poppee
$poppee_URL="https://mvtbib.adc.education.fr/mvtbib/servlet/mvtbib.Centrale";
$nom_fichier_sortie='fichier_sortie.csv';
class RecupHeaders {
# classe pour récupérer le header d'une requête et donc les cookies
# code inspiré de Geoffrey Warnants http://geoffray.be/blog/php/extraire_http_headers_avec_lib_curl
# et Brian Wendt http://ontodevelopment.blogspot.fr/2011/04/curloptheaderfunction-tutorial-with.html
protected $_url = null;
protected $_headers = array();
protected $_body = '';
public function __construct($url,$headers,$postfields) {
# initalise l'objet
$this->_url = curl_init($url);
curl_setopt($this->_url, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->_url, CURLOPT_HEADER, true);
curl_setopt($this->_url, CURLOPT_HTTPHEADER, $headers);
curl_setopt($this->_url, CURLOPT_POST, true);
curl_setopt($this->_url, CURLOPT_POSTFIELDS, $postfields);
# fonction readHeaders appelée à la réception de chaque ligne d'entête
curl_setopt($this->_url, CURLOPT_HEADERFUNCTION,array($this, 'readHeaders'));
}
public function __destruct() {
curl_close($this->_url);
}
public function getHeaders() {
# exécute la requête et remplit le tableau $headers
$this->_body = curl_exec($this->_url);
return $this->_headers;
}
public function getBody() {
return $this->_body;
}
protected function readHeaders($url, $str) {
# ajout de chaque ligne d'entête dans un tableau
if (strlen($str) > 0) {
$this->_headers[] = $str;
}
# la fonction doit retourner le nombre précis de caractères de chaque ligne d'entête
return strlen($str);
}
}
function get_session_cookie ($url) {
# simule une navigation et récupère le cookie
$headers=array();
$session_cookies=array();
# page d'accueil de Poppee -> 1er cookie
$connexion = new RecupHeaders($url,array(''),'');
$headers=$connexion->getHeaders();
$connexion= null;
foreach ($headers as $header) {
if (preg_match('/^Set-Cookie:\s*JSESSIONID=([^;]*)/mi', $header, $cookie) == 1) {
$session_cookies[]=$cookie[1];
}
}
if (count($session_cookies) <> 1) {
# erreur
return 0;
}
$cookie1=$session_cookies[0];
# 2e page de Poppee -> 2e cookie
$connexion = new RecupHeaders($url,array('Cookie: JSESSIONID='.$cookie1,'Referer: https://mvtbib.adc.education.fr/mvtbib/servlet/mvtbib.Centrale','Connection: keep-alive',
'Host: mvtbib.adc.education.fr','Content-Type: application/x-www-form-urlencoded'),'pageAction=mouvements&lastPage=identification&login=Y%2Bbidon&password=bidon&profil=&sessionInvalidate=&ecran=&motCle=');
$headers=$connexion->getHeaders();
$connexion= null;
$session_cookies=array();
foreach ($headers as $header) {
if (preg_match('/^Set-Cookie:\s*JSESSIONID=([^;]*)/mi', $header, $cookie) == 1) {
$session_cookies[]=$cookie[1];
}
}
if (count($session_cookies) <> 1) {
# erreur
return 0;
}
$cookie2=$session_cookies[0];
# 3e page
$connexion = new RecupHeaders($url,array('Cookie: JSESSIONID='.$cookie2,'Referer: https://mvtbib.adc.education.fr/mvtbib/servlet/mvtbib.Centrale','Connection: keep-alive',
'Host: mvtbib.adc.education.fr','Content-Type: application/x-www-form-urlencoded'),'pageAction=mouvements&lastPage=mouvements&codeMvt=CONSER');
$headers=$connexion->getHeaders();
$connexion= null;
return $cookie2;
}
function get_profil($noRet,$uaarneRet,$session_cookie) {
# récupère une fiche de profil de poste
$x = curl_init("https://mvtbib.adc.education.fr/mvtbib/servlet/mvtbib.Centrale");
curl_setopt($x, CURLOPT_HTTPHEADER, array(
'Connection: keep-alive',
'Host: mvtbib.adc.education.fr',
'Referer: https://mvtbib.adc.education.fr/mvtbib/servlet/mvtbib.Centrale',
'Content-Type: application/x-www-form-urlencoded',
'Cookie: JSESSIONID='.$session_cookie));
curl_setopt($x, CURLOPT_HEADER, 0);
curl_setopt($x, CURLOPT_POST, 1);
curl_setopt($x, CURLOPT_POSTFIELDS, 'pageAction=listeProfils2&lastPage=listeProfils2&uaarneRet='.$uaarneRet.'&noRet='.$noRet.'&action=detailler&radiobutton=radiobutton');
curl_setopt($x, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($x, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($x, CURLOPT_COOKIESESSION, 1 );
$data = curl_exec($x);
curl_close($x);
return $data;
}
function get_liste_profils($session_cookie) {
# récupère une fiche de profil de poste
# renvoie un tableau de tableau à 4 entrées : numéro de poste, code établissement, nom établissment, ville
$tableau=array();
$x = curl_init("https://mvtbib.adc.education.fr/mvtbib/servlet/mvtbib.Centrale");
curl_setopt($x, CURLOPT_HTTPHEADER, array(
'Connection: keep-alive',
'Host: mvtbib.adc.education.fr',
'Referer: https://mvtbib.adc.education.fr/mvtbib/servlet/mvtbib.Centrale',
'Content-Type: application/x-www-form-urlencoded',
'Cookie: JSESSIONID='.$session_cookie));
curl_setopt($x, CURLOPT_HEADER, 0);
curl_setopt($x, CURLOPT_POST, 1);
curl_setopt($x, CURLOPT_POSTFIELDS, 'pageAction=chEtab5&lastPage=chEtab5&uaarne=&codeDpt=&ttEtab=1');
curl_setopt($x, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($x, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($x, CURLOPT_COOKIESESSION, 1 );
$data = curl_exec($x);
curl_close($x);
$dom = new DOMDocument();
@$dom->loadHTML($data);
$xpath = new DOMXpath($dom);
$nb_tr=0;
$trs=$xpath->query('/html/body/table/tr/td[2]/div/form/table/tr');
foreach ($trs as $tr) {
if (($nb_tr > 0) and (($nb_tr+2) < $trs->length)) {
$content = $tr->childNodes->item(4)->nodeValue;
$num_poste = trim($tr->childNodes->item(6)->childNodes->item(0)->nodeValue);
$detail=explode("\n",$content);
$code=trim($detail[1]);
$etab=trim($detail[2]);
$ville=trim($detail[3]);
$tableau[]=array($num_poste,$code,$etab,$ville);
}
$nb_tr++;
}
return $tableau;
}
# ============================
# Corps du programme
# ============================
$fichier_sortie = fopen($nom_fichier_sortie, 'w');
# récupération du cookie de session
$session_cookie= get_session_cookie ($poppee_URL);
if ($session_cookie ===0) {
echo "Erreur dans la fonction get_session_cookie\n";
exit;
}
# récupération de la liste de tous les profils (mais pas du détail)
$liste_profils = get_liste_profils($session_cookie);
# boucle : récupération des profils
foreach ($liste_profils as $profil_court) {
$numero = $profil_court[0];
$code = $profil_court[1];
$etab = $profil_court[2];
$ville = $profil_court[3];
$profil = get_profil($numero,$code,$session_cookie);
$erreur=strpos($profil,"Veuillez-vous reconnecter !");
if ($erreur > 0) {
echo "Erreur : le cookie fourni est invalide\n";
exit;
}
$erreur=strpos($profil,"Veuillez recommencer");
if ($erreur > 0) {
echo "Erreur : le cookie fourni est invalide (ou autre problème)\n";
exit;
}
# nettoyage des espaces
$profil = preg_replace('/\s+/', ' ', (str_replace("\n", ' ', $profil)));
# transformation en arbre DOM
$dom = new DOMDocument();
@$dom->loadHTML($profil);
$xpath = new DOMXpath($dom);
$descriptif_court = trim($xpath->query("/html/body/table/tr/td[2]/div/form/table/tr[4]/td[3]")->item(0)->nodeValue);
$descriptif_long = trim($xpath->query("/html/body/table/tr/td[2]/div/form/table/tr[6]/td[3]")->item(0)->nodeValue);
$responsabilite = trim($xpath->query("/html/body/table/tr/td[2]/div/form/table/tr[8]/td[3]")->item(0)->nodeValue);
$competences = trim($xpath->query("/html/body/table/tr/td[2]/div/form/table/tr[10]/td[3]")->item(0)->nodeValue);
$ligne= array(
$numero,
$code,
$etab,
$ville,
$descriptif_court,
$descriptif_long,
$responsabilite,
$competences
);
# écriture dans un fichier csv
$delimiter=",";
$enclosure='"';
fputcsv($fichier_sortie, $ligne,$delimiter,$enclosure);
sleep (2);
}
fclose($fichier_sortie);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment