View Full Version : Proxy for AJAX

07-09-2007, 12:16 AM
Hi all,
while trying to use AJAX remotely, after a few difficulties, a friend gave me the code that follows to make it works as a proxy. now it all works, but iwanted to know if there is any alternative to do this, and why choose one or another... a link to somegood explanation would be good enough.. thanks

$url = ($_POST['url']) ? $_POST['url'] : $_GET['url'];

// Open the Curl session
$session = curl_init($url);

// If it's a POST, put the POST data in the body
if ($_POST['url']) {
$postvars = '';
while ($element = current($_POST)) {
$postvars .= key($_POST).'='.$element.'&';
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $postvars);
// 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. Set the Content-Type appropriately
header("Content-Type: text/xml");
echo $xml;

07-10-2007, 02:16 AM
JavaScript has the same domain policy so you can not retrieve stuff of other sites. There is a work around with JSON if the website you are pulling the data from has it.


07-10-2007, 10:58 PM
and what about this workaround with JSON?
do i have to read your book :) or can i find any info somewhere else..? in the meantime i'm starting searching for it..