Reputation: 1749
I'm using PHP and xpath to parse some HTML pages: in a previous issue (rif. Parsing an HTML page using curl and xpath in PHP), I've solved how to parse a page to extract some values.
Now I've another page in which, before to get the values I'd like to parse, I've to select a value (Venezia in the picture ..., in the combobox "Provincia" ... ), and then to click on a button ("CERCA" in the picture ...), before to get the values I'd like to parse (that are the numbers in red, green and yellow boxes in the page ....)
The url page is the follow
https://salute.regione.veneto.it/servizi/situazione-nei-pronto-soccorso
and here you're the image of the page after the selection and action described above
Is it possible, and how, in PHP, to simulate this HTML navigation sequence to obtain the HTML page than I've to parse?
Upvotes: 0
Views: 560
Reputation: 101
In PHP, you can use curl to post the data of the form at the same url that the form action: https://salute.regione.veneto.it/servizi/situazione-nei-pronto-soccorso?p_p_id=PRONTOSOCCORSO_WAR_portalprontosoccorso_INSTANCE_o0QZ&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&p_p_col_id=column-3&p_p_col_count=1
and get back the HTML page.
Example of php script, inspiré de https://davidwalsh.name/curl-post (you have to install curl to get working this example):
<?php
$url = 'https://salute.regione.veneto.it/servizi/situazione-nei-pronto-soccorso?p_p_id=PRONTOSOCCORSO_WAR_portalprontosoccorso_INSTANCE_o0QZ&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&p_p_col_id=column-3&p_p_col_count=1';
$fields = array(
'ulss' => '101',
'provincia' => 'BL',
'nomPS' => '',
'rossoInAttesa' => '',
'gialloInAttesa' => '',
'verdeInAttesa' => '',
'biancoInAttesa' => ''
);
//url-ify the data for the POST
$fields_string = "";
foreach($fields as $key=>$value) {
$fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//execute post
$result = curl_exec($ch);
file_put_contents('result_page.html', $result);
//close connection
curl_close($ch);
Upvotes: 1