Calling and displaying content from another domain hosted on another machine

Discussion in 'Site & Server Administration' started by hklein, May 27, 2010.

  1. #1
    Hello,

    I just hope it's the good section of the forum...

    Here is the problem :

    I've two sites www.sitea.com and www.siteb.com
    They are hosted on different servers, different IPs

    I would like to display on sitea.com HTML (text and images) content stored on siteb.com

    Virtual include won't work because of different servers.
    Nor ssi
    Iframes could, but I don't want to use it
    I could do something like storing the info in a mysql db and call it, but this seems to ask some work. :)

    So do you, by chance, know a function or a tool that could allow me to do that ?

    Thanks a lot !
     
    hklein, May 27, 2010 IP
  2. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #2
    Images are easy just give the full URL of the image on siteb then when people view sitea they will view the page there and pull the images from siteb.

    Content is trickier:

    - store in a database and query remotely
    - store the text fragments in a file which you can access via http then you can use PHP + curl to fetch the file and echo it to the browser. You can even do <?php include('http://www.example.com/fragment.html'); ?> but if something goes wrong the timeout is generally too long where as with curl you can control it, detect it and act appropriately.

    If you use either of those I'd cache the text locally to avoid having to fetch it on every call as that would just slow the page.
     
    tolra, May 27, 2010 IP
  3. hklein

    hklein Active Member

    Messages:
    43
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    73
    #3
    Thank you for your ideas Tolra,

    it just seems the php trick <?php include('http://www.example.com/fragment.html'); ?> doesn't work due to server configuration.

    I don't know how curl works so I'll try with a mysql call and remote sql access.

    If you've another idea, let's share.
     
    hklein, May 27, 2010 IP
  4. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #4
    Sorry, I should have mentioned the include is dependent on the server settings.

    For curl something like the following should get a page:
    <?php
    $ch = curl_init('http://www.example.com/something.html');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    echo(curl_exec($ch));
    curl_close($ch);
    ?>
    
    PHP:
    There's no error checking in there and no caching therefore every page view will cause data to be read from the remote server.
     
    tolra, May 27, 2010 IP
  5. hklein

    hklein Active Member

    Messages:
    43
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    73
    #5
    Jesus it works.

    There's one problem left:

    Style is not respected : images which are centered in the source file are left aligned in the calling system and breaks between paragraphs are not there anymore.

    Besides this, if your code is standard to all shared hostings, I think we can close this issue.
     
    hklein, May 27, 2010 IP
  6. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #6
    You'll need to copy the elements from the style sheet on the source site to the destination sites style sheet.

    curl should work on most hosts however as always there's some that won't support it, if the host won't allow curl move to a different host.
     
    tolra, May 27, 2010 IP
  7. hklein

    hklein Active Member

    Messages:
    43
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    73
    #7
    Thanks, It indeed works.
    I've learned some interesting things.

    I see you've servers in UK, you'll probably hear from me in a near future.
     
    hklein, May 27, 2010 IP
  8. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #8
    Glad to be of help.

    I'll finish with if you are going to use this in a production environment and with a reasonable number of visitors you really need to look at caching the fragments you are pulling from the remote server or sooner or later your site will crawl and/or your host will have a word with you about optimising the scripts.

    Caching can be as simple as save the fragment into a file, on the next view check the time the file was last modified and if older than 60 minutes fetch from the remote server otherwise display the contents of the file (see readfile).

    The other thing to remember is if the remote host is down then you have no content, however if you cache the fragments then you can detect the error and reuse the cached data thus keeping one site online all be it with old data.
    It's worth you keeping an eye on http://atlantichosting.net/specials.html or http://twitter.com/atlantichosting as we do run specials periodically.
     
    Last edited: May 27, 2010
    tolra, May 27, 2010 IP