batch/1.1/old/getBodaccPdf.php
2012-10-16 07:44:31 +00:00

97 lines
3.1 KiB
PHP

#!/usr/bin/php -c/var/www/batch/config/php_batch_sd.ini
<?php
//Define cookie file for storage
$ckfile = '/tmp/cookies.txt';
if (file_exists($ckfile)) unlink($ckfile);
if ($argc != 4){
echo $argv[0].' type annee num';
echo "\n";
exit;
}
$type = strtoupper($argv[1]);
$annee = $argv[2];
$num = $argv[3];
$c = strlen($num);
for($i=0;$i<4-$c;$i++){
$num = '0'.$num;
}
$annonce = 1;
function getPage($url, $curl_data = '', $verbose=false)
{
global $ckfile;
//$user_agent = 'Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1';
//$user_agent = 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)';
$user_agent = 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)';
$post = false;
$fields = '';
if (is_array($curl_data) && count($curl_data)>0) {
foreach($curl_data as $key=>$value) {
$fields .= $key.'='.$value.'&';
}
rtrim($fields,'&');
$post = true;
}
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => $user_agent, // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_POST => $post, // i am sending post data
CURLOPT_POSTFIELDS => $fields, // this are my post vars
CURLOPT_SSL_VERIFYHOST => 0, // don't verify ssl
CURLOPT_SSL_VERIFYPEER => false, //
CURLOPT_VERBOSE => $verbose , //
//CURLOPT_COOKIESESSION => true,
CURLOPT_COOKIEFILE => $ckfile,
CURLOPT_COOKIEJAR => $ckfile, // Stockage du cookie de session
);
$ch = curl_init($url);
curl_setopt_array($ch,$options);
$content = curl_exec($ch);
$err = curl_errno($ch);
$errmsg = curl_error($ch) ;
$header = curl_getinfo($ch);
curl_close($ch);
// $header['errno'] = $err;
// $header['errmsg'] = $errmsg;
// $header['content'] = $content;
return array('header'=>$header, 'content'=>$content);
}
//==> Start
$debug = false;
echo $url = "http://www.bodacc.fr/index.php?action=voir&p=$type&n=$annee$num&a=$annonce";
echo "\n";
$result = getPage($url);
if ($debug) file_put_contents('bodacc1.html', $result['content']);
preg_match('/\<a href="index\.php\?action=pdf&amp;index=([0-9]{1,})"\>/', $result['content'], $matches);
echo $url = "http://www.bodacc.fr/index.php?action=pdf&index=".$matches[1];
echo "\n";
$result = getPage($url);
if (substr($result['content'],0,4)!='%PDF'){
if ($debug) file_put_contents('bodacc2.html', $result['content']);
echo 'Erreur : pas de PDF'.EOL;
exit;
}
echo strlen($result['content']).' octets'.EOL;
file_put_contents("BODACC_".$type."_".$annee."_".$num.".pdf", $result['content']);
die();
?>