Ho fatto questo script, ho un problema tra le variabili $host e $path,dato che il sito in questione utlizza come primo accesso un dominio che inizia con "www",in $path,quando vado a costruire la richiesta post, ho dovuto inserire per intero la parte "http://www1.agenziaentrate.it" che inizia con "www1" invece che con "www",ovviamente,quando lo eseguo,mi da' pagina non trovata (errore http 404).
Ho evidenziato in rosso le parti di codice di interesse,spero che vi sia una soluzione.
<?php
// preconditions
$port = 80;
$path = "http://www1 .agenziaentrate.it/servizi/tassazioneattigiudiziari/registrazione.htm";
$host = "www .agenziaentrate.it";
$method = "POST";
$contenttype = "text/html";
//$data = "passo=0";
$data = "action=scegliufficio";
// script
if($port == 443)
$sslhost = "ssl://".$host;
else
$sslhost = $host;
//$fp = fsockopen($sslhost, $port);
echo($sslhost);
//$fp = fsockopen($sslhost, $port);
$fp = fsockopen($sslhost, $port, $errorNumber, $errorString);
if (!$fp)
{
echo("Failed opening http socket connection:".$errorString." (" .$errorNumber.")
\n");
}
echo("dopo fsockopen");
//http://www1.agenziaentrate.it/servizi/tassazioneattigiudiziari/registrazione.htm
$requestHeader = $method." ".$path ." HTTP/1.1\r\n";
$requestHeader.= "Host: ".$host ."\r\n";
$requestHeader.= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0\r\n";
$requestHeader.= "Content-Type: text/html\r\n";
if ($method == "POST")
{
$requestHeader.= "Content-Length: ".strlen($data)."\r\n";
}
$requestHeader.= "Connection: close\r\n\r\n";
if ($method == "POST")
{
$requestHeader.= $data;
}
fwrite($fp, $requestHeader);
echo("dopo fwrite ".$requestHeader);
$responseHeader = '';
$responseContent = '';
do
{
$responseHeader.= fread($fp, 1);
}
while (!preg_match('/\\r\\n\\r\\n$/', $responseHeader));
if (!strstr($responseHeader, "Transfer-Encoding: chunked"))
{
while (!feof($fp))
{
$responseContent.= fgets($fp, 128);
}
}
else
{echo("non riuscita");
while ($chunk_length = hexdec(fgets($fp)))
{
$responseContentChunk = '';
echo("debug 01 ".$chunk_length);
$read_length = 0;
while ($read_length < $chunk_length)
{
$responseContentChunk .= fread($fp, $chunk_length - $read_length);
$read_length = strlen($responseContentChunk);
}
$responseContent.= $responseContentChunk;
fgets($fp);
}
}
echo("debug AF ".$responseContent);
echo(" ok 01 ?");
?>