SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question AJAX/php - plugin cross site request

    Heya, I've recently been working on a small AJAX/PHP plugin, its very easy to understand, you just call the page and it returns a quote from a database in html, something like

    <strong>quote here.. <em>author name</em></strong>

    And then you style it however you want, its good for semantics that way, you would typically wrap it in a div and update that div with the output each time from your ajax.

    It all works fine, and you can see an example here @ www.theshadownest.com.au click on the clock, and you will see the quote come up. disregard the other stuff in the box.


    The idea i had for this was set up a large database of quotes with simple semantic output and let Ajax queries call that page, but i want to be able to call that page output from other servers, urls etc... I thought that would be fine, you just request the page with ajax on your site and it could return the response from my site...

    But this does not work, you get a permission denied error in the Javascript, it obviously can't do this.


    How do you write a PHP/AJAX plugin that can work from multiple sites, because i want to be able to let anyone make a request, its basically a resource for people who want to add quotes to their site without setting up their own database or writing their own php.


    Can someone help me out?

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    IE allows cross domain calls but Firefox doesn't. You can use a Cross Domain Hack (i've not used it) or you can create a server side object that does the remoting and returns the value.


  3. #3
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gRoberts View Post
    IE allows cross domain calls but Firefox doesn't. You can use a Cross Domain Hack (i've not used it) or you can create a server side object that does the remoting and returns the value.
    Yeah you have to sign your script or something for it to work in firefox, doesn't work in ie at all for me though, just gets permisiion denied anyway.

    So creating a server side object for remoting, how do i do this? Any help is appreciated

  4. #4
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well you need a server side language, PHP, ASP etc. Once you have a decided language, you could get much more help from there.

    IIRC, using PHP & cURL

    Code:
    <?php
    
        if(isset($_REQUEST['Url'])) {
            $curl_handle=curl_init();
            curl_setopt($curl_handle,CURLOPT_URL,$_REQUEST['Url']);
            curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
            curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
            $buffer = curl_exec($curl_handle);
            curl_close($curl_handle);
    
            if (empty($buffer))
            {
                print "<p>Sorry, example.com are a bunch of poopy-heads.</p>";
            }
            else
            {
                print $buffer;
            }
    
        }
    
    ?>


  5. #5
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ALright just so i understand,

    I'd use my ajax button to call a PHP page with your above code in it on the remote server, URL is the URL to my PHP page on my main server which grabs and retuns the entires from the table?


    yeah yeah?


  6. #6
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in your ajax code, put in the url to the php document containing the code above, and give the page an querystring value of the original url you wanted to access.

    ie

    Code:
    request.open("GET", "http://www.web.com/resource.xml", true);
    // changes to
    request.open("GET", "proxy.php?Url=http://www.web.com/resource.xml", true)
    Then obviously create a php page called proxy.php and put the php code in. Then because the php page is local, the ajax script won't complain.


  7. #7
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,115
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)
    Hi,

    XMLHttpRequest doesn't let you open files on another server, but the script tag allows it.

    Code javascript:
    <script type="text/javascript" src="http://www.web.com/resource.xml"></script>

    You can use the DOM to create the script element and append it to the head node from your AJAX response handler.

  8. #8
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gRoberts View Post
    in your ajax code, put in the url to the php document containing the code above, and give the page an querystring value of the original url you wanted to access.

    ie

    Code:
    request.open("GET", "http://www.web.com/resource.xml", true);
    // changes to
    request.open("GET", "proxy.php?Url=http://www.web.com/resource.xml", true)
    Then obviously create a php page called proxy.php and put the php code in. Then because the php page is local, the ajax script won't complain.

    Perfect! Works!

    Is this a hack or is it legal?


  9. #9
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its perfectly legal unless you start using it to claim content as your own. Its a hack only because its fooling the Ajax code into thinking the data coming from an internal source rather then the original external source.


  10. #10
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Double plus good winston.

    Thanks very much much gman, very helpful, this forum needs cudos points

  11. #11
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your more then welcome, thank you for taking the time to thank me. Not many people do that these days.


  12. #12
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gRoberts View Post
    Your more then welcome, thank you for taking the time to thank me. Not many people do that these days.
    Yeah its all very 'wam, bam, thank you mam', these days.


Bookmarks

Posting Permissions

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