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
|
class HtmlCurlReader {
protected $curl;
protected $session;
public function __construct() {
$this->curl = curl_init();
}
public function __destruct () {
curl_close($this->curl);
}
public function getHtml($url, $data, $cookie) {
$this->session = $cookie;
curl_setopt_array($this->curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_USERAGENT => "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) Chrome/57.0.29",
CURLOPT_COOKIESESSION => true,
CURLOPT_COOKIEFILE => $this->session,
CURLOPT_COOKIEJAR => $this->session,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYPEER => false,
)
);
$out = curl_exec($this->curl);
// todo: gestion des erreurs ici
return $out;
}
public function getSession()
{
return $this->session;
}
}
$res = new HtmlCurlReader();
$data = "id=monidpass=monpass";
$cookie = tmpfile();
$url = "mon url de connexion";
$html = $res->getHtml($url,$data, $cookie);
$data2 = "champsun=1&champs_deux=2";
$cookie2 = $res->getSession();
$res2 = new HtmlCurlReader();
$url2 = "www.mapageB.com";
$html2 = $res2 ->getHtml($url2,$data2, $cookie2);
echo $html2; |
Partager