Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Proxy for AJAX

  1. #1
    New Coder
    Join Date
    Mar 2007
    Location
    italy
    Posts
    94
    Thanks
    1
    Thanked 1 Time in 1 Post

    Proxy for AJAX

    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

    Code:
    <?php
    $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.'&';
    		next($_POST);
    	}
    	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;
    curl_close($session);
    ?>

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    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.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    New Coder
    Join Date
    Mar 2007
    Location
    italy
    Posts
    94
    Thanks
    1
    Thanked 1 Time in 1 Post
    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..


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •