0

There is a file on another site that I do not own, with a URL in the following format:

http://example.com/path/with/some/variables

I want to include this file in one of my own pages. I could use iframe to do this, but I also want to change the CSS of something within the included file. To my knowledge, I can't do this with this method.

However, I can't seem to be able to successfully add this via PHP either, with something like:

<?php include 'http://example.com/path/with/some/variables'; ?>

I'm not sure what other methods exist that can do this, but surely this must be possible.

Also, I'm aware of the security implications of using include in a situation like this.

Hiigaran
  • 829
  • 10
  • 28
  • 1
    Do you want to just dump the content of external url inside your page? – Javad May 07 '14 at 21:50
  • What is that file? A piece of HTML? Just a CSS file ? Something else ? – Andrei May 07 '14 at 21:52
  • Correct. It's got HTML, CSS, and some JS within it. Basically, whatever you would see in the source of the page is what I'm trying to display. – Hiigaran May 07 '14 at 21:52
  • If you access a PHP script through HTTP, it runs the script and returns the output, it doesn't copy the PHP source. – Barmar May 07 '14 at 21:52
  • 1
    Then use `file_get_content($url);` to get the all content as string and dump it wherever you want [http://ca3.php.net/manual/en/function.file-get-contents.php] – Javad May 07 '14 at 21:54
  • What's the difference between that function, and the readfile one that Barmar posted? – Hiigaran May 07 '14 at 21:55
  • 1
    http://stackoverflow.com/questions/20095175/php-readfile-vs-file-get-contents this is the difference – Andrei May 07 '14 at 21:56
  • Hah, I just finished reading that exact page! Thanks anyway. – Hiigaran May 07 '14 at 21:57

3 Answers3

3

Use readfile:

<?php readfile('http://example.com/path/with/some/variables'); ?>
Barmar
  • 741,623
  • 53
  • 500
  • 612
3

Yeah, security limitations won't allow you do do this directly in an iframe, by manipulating the DOM of the iframed file.

To do it in PHP, you could create a PHP script to read the contents of the URL and add an external CSS file that you've created, to override whatever you want. So:

myreader.php:

$contents = file_get_contents("http://example.com/path/with/some/variables");
$contents = preg_replace("/<head>/", "<head>\n<link rel='stylesheet' type='text/css' href='mystyle.css'>", $contents, 1);

echo $contents;

and then create mystyle.css:

body {
  color : red !important;
}

Finally, either just point your browser to myreader.php, or if you still want it in an iframe, point the iframe src to myreader.php.

PS: Stealing is wrong :)

Cully
  • 6,427
  • 4
  • 36
  • 58
  • Don't worry, it's not stealing. It's a publicly available resource that is normally meant to be used via iframes. I just wanted to make a slight edit to the styling. Either way, if I already have a CSS file on the page this is meant to be displayed in, I can simply add the relevant styles there, right? – Hiigaran May 07 '14 at 21:59
  • I was just kidding :) I don't think you can style an iframe's contents from the page including the iframe. Though, you can style the iframe (i.e. its border, etc.). – Cully May 07 '14 at 22:01
2

You can use file_get_contents

<?php $content = file_get_contents('http://example.com/path/with/some/variables'); ?>

Here is the documentation file_get_contents

Javad
  • 4,339
  • 3
  • 21
  • 36