views:

75

answers:

6

How to display other sites content in our sites with out using iframe? Is it possible to load one sites file details in our site with out using the iframe. any body knows the solution please help me with a sample code?

A: 

The way I have achieve this in the past is as follows

<?php
$handle = fopen("http://someurl", "rb");
$contents = stream_get_contents($handle);
fclose($handle);

echo "<div>Your Content</div>";
echo "<div>";
print($contents);
echo "</div>";
echo "<div>More of your contents";

?>
Codemwnci
Please be careful when using this code though. As @PaulTomblin has pointed out, using content without permission is stealing. At least using frames, it gives the calling site a chance to use javascript to prevent it.
Codemwnci
Why not just using file_get_contents on the url, turns 3 lines into 1.
balupton
+1  A: 

You can use the PHP Curl library.

Gazler
+1  A: 

It is possible and quite easy with YQL.

The Yahoo! Query Language is an expressive SQL-like language that lets you query, filter, and join data across Web services. With YQL, developers can access and shape data across the Internet through one simple language, eliminating the need to learn how to call different APIs.

galambalazs
A: 

jQuery should be able to do this (I'm assuming you're using jQuery since you've tagged the question with it). You should look at the documentation on the .load() method as that has an excellent example in there

Edit: As Nick points out below, this will only work for pages on the originating site due to the same origin policy in javascript

Dave
You can't use `.load()` here, check out the [same-origin policy](http://en.wikipedia.org/wiki/Same_origin_policy) which prevents this for security reasons :)
Nick Craver
@Nick Craver Oops, you're totally right. Thanks for pointing this out, editing my answer to reflect this
Dave
A: 

You cannot use Ajax due to the Same Origin policy. There are two ways to circumvent this.

  • Use a proxy - As Gazler said, you can create a proxy script in PHP to download the file. Call the file proxy script from your javascript.

  • Use JSONP - You can use JSONP for cross domain communication. Make sure you read the cautionary note

Joyce Babu
Any idea why this was down voted? I would like to know what was wrong with my approach.
Joyce Babu
A: 

You can achieve it with the code below. Two notes though:

1) I'm not sure this works with IE

2) Heed the advice put out by @Paul_Tomblin and @Codemwnci about getting permission to display a different site's content.

The html part is:

<object id="external-page" type="text/html" data="" > 

</object>

The javascript part in jquery is:

$("#external-page").attr("data", "http://foreign-site.net/foreign-page.html");
kalengi