Buongiorno.
Sto lavorando, a causa di un progetto universitario, su ajax e quindi avrei sbagliato sezione. Il problema però è che mi trovo a dover pescare dei dati xml da un servlet deployata su un tomcat da una pagina su un server apache, ed essendo xml non posso pescarli direttamente con ajax.
Devo quindi instradare il tutto tramite un proxy php (lo ho scoperto grazie a questo e questo. Il problema però è che non ho alcuna nozione di php.
Il mio xml è ad un indirizzo del tipo http://myServlet/SmartClassGrapher?action=3 e finora gestivo la cosa, con ajax, in questa maniera
Codice PHP:
xmlhttp.open("GET","http://myServlet/SmartClassGrapher?action=3",true);
xmlhttp.send();
ora quindi dovrei usare al posto del mio indirizzo un proxy.php che dovrebbe essere qualcosa tipo uno di questi due
Codice PHP:
// Hardcode the hostname
define('HOSTNAME', 'http://api.local.yahoo.com/');
// Get the REST call path from the AJAX application
$path = $_GET['path'];
// The URL to fetch is the hostname + path
$url = HOSTNAME.$path;
// Open the Curl session
$session = curl_init($url);
// Don't return HTTP headers. Do return the contents of the call
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// Make the call
$xml = curl_exec($session);
// The web service returns XML
header("Content-Type: text/xml");
echo $xml;
curl_close($session);
Codice PHP:
function send($url) {
$params = array('http' => array(
'method' => 'POST'
));
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
Che dovrei modificare, ma non ho la più pallida idea né di come chiamarli dal javascript né di come gestire all'interno del proxy i dati del GET o del POST (la action=3) (per accedere alla mia risorsa posso al limite usarli entrambi)