ngmir
ngmir

Reputation: 498

Fetching content from Website on another Server

What i basically want to do is to get content from a website and load it into a div of another website. This should be no problem so far. The problem is, that the content that should be fetched is located on a different server and i have no source access to it.

I'd prefer a solution using JavaScript of jQuery. Can i use a .htacces redirect to fetch the content from a remote server with client-side (js) techniques?

I will also go with other solutions though. Thanks a lot in advance!

Upvotes: 0

Views: 1625

Answers (4)

Mottie
Mottie

Reputation: 86473

You will likely have to "scrape" the data you need and store it on your server.

This is a great tutorial on how to cache data from an external site. It is actually written to fetch and store XML, so it'll need some modification. Also, if your site doesn't allow file_get_contents then you may have to modify it to use cUrl.

Upvotes: 0

dthorpe
dthorpe

Reputation: 36082

If the content from the other website is just an HTML doc that you want to display on your site, you could also use an iframe to pull it in. You won't have access to any of its content because of browser security rules.

Upvotes: 0

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114417

You can use a server-side XMLHTTP request to grab your content from the other server. You can then parse it on you server (A.K.A screen-scraping) and serve-up the portion you want along with your web page.

Upvotes: 1

friedo
friedo

Reputation: 67048

You can't execute an AJAX call against a different domain, due to the same-origin policy. You can add a <script> tag to the DOM which points at a Javascript file on another domain. If this JS file contains some JSON data that you can use, you're all set.

The only problem is you need to get at the JSON data somehow, which is where JSON-P callbacks come into the picture. If the foreign resource supports JSON-P, it will give you something that looks like

your_callback( { // JSON data } );

You then specify your code in the callback.

See JSONP for more.

If JSONP isn't an option, then the best bet is to probably fetch the data server-side, say with a cron job every few minutes, and store it locally on your own site.

Upvotes: 3

Related Questions