Code Snippet: Wrapping content from another site on your site

Sponsored Links

Code Snippet: Wrapping content from another site on your site

Some time ago, there was a question on the Drupal forums about wrapping content from another site to your site.

Here is a basic PHP code snippet that would do just that. You may need to fiddle with the HTML to make the paths absolute to the site that you are wrapping.

If you have improvements, please let me know. 

Also, please do not use this to leech content from other sites, and obey their copyright rules. 

Method 1: Using fopen on a URL 

$url = "http://example.com";

$handle = @fopen ( $url, "r" );
if ( $handle ) {
while ( true ) {
$data = fgets ( $handle, 4096 );
print $data;

if ( true == feof( $handle ) ) {
break;
}
}

fclose ( $handle );
}
else {
$result .= t("Could not contact host: ") . $url;
}

Method 2: Using fopen on a URL - more concise way

After writing the above, Drupal hacker par excellance, Karoly Negyesi (a.k.a. chx), found  a much more elegant and simple solution:

$url = "http://example.com/page.html";
print file_get_contents($url); 

This is much less code for sure. 

Method 3: Using IFRAMEs

You can use an IFRAMEs to wrap content from another site, or another application on your site.

To do this, all you need is to paste the following HTML snippet in the block or the node you want. You will have to experiment with the height and width until you get the size that works best for the site you are wrapping.

<iframe
src="http://www.example.com"
Frameborder=0
height="600"
width="640">
</iframe>

I tried the iframe and I get

I tried the iframe and I get the following php error: Parse error: parse error, unexpected '<' in /home/.../drupal-4.6.3/includes/common.inc(1857) : eval()'d code on line 2

Any ideas?

I tried the other snippets and the css gets messed up on my main drupal content. It seems to want to add the wrapped page's css into my drupal css or something...

What format?

Did you use PHP format? Just use HTML.
--
Khalid Baheyeldin

I-frame in drupal node

I'm trying the iframe snippet, but I'm also getting the unexpected "<" error. I have the php input format selected and I've added closing and opening php tags to the code.

Contact me

Go to the feedback form and copy/paste the code you are trying, and I will look into it, and reply by email.
--
Khalid Baheyeldin

Code Snippet:Wrapping content from another..

Can you help me...

I have tried the PHP method (gives me nothing but code, even when set to PHP code) and the Iframe method to capture and display another (exterior) web page within my Drupal site. I am using the adc theme.

The Iframe method seems to work fine in FireFox but in IE the node overshoots the right side...falling under the blocks on the right column of the page.

See what I mean...

http://reelorbit.com/node/27/

Please advise...

CSS issue

Looks like a CSS issue. Try another theme and see if it works or not. If it does, then it an adc specific CSS bug.
--
Khalid Baheyeldin

Works!

Thanks for the help. It works now. I didn't realize I wasn't supposed to wrap the i-frame in php tags. Without those php tags, it functions perfectly!

Where do you plug all this in?

This is what I need but I have no clue where to input all this code in. New to Drupal.

Thanks!

In a node

Create a node, set the input format to PHP code, and paste the code in it.
--
Khalid Baheyeldin

I created a node (page) and don't see some of the options...

I did "create content" ==> "page" and chose "full HTML" instead of "filtered HTML". I didn't see an option for setting the input to PHP code.

I also tried to create a node via views. Still didn't see the option. Can you provide a little more info on how to create this node, which I'm going to select as having the input format of "PHP code"?

Thanks :)

Post new comment

  • All spam and irrelevant comments will be deleted.
  • Comments posted here will take some time to appear on the site. Do not post your comment again if you do not see it. Just be patient and it will be published.
  • Note that what you post here will be publicly available on the web and will be indexed in search engines.
  • We reserve the right to unpublish any comments without stating the reasons for that.
  • All postings are subject to our Terms of use
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote>
  • Lines and paragraphs break automatically.

More information about formatting options