Include HTML OUTPUT of another page into a PHP variable?

How can I set the value of a PHP variable to be the HTML OUTPUT of another page into a PHP variable?

For example I can get TWEETS directly as a JSON object from Twitter by going to a user’s URL. But rater than use AJAX, I would like to just include the data directly into my page as it’s being built by my PHP script. This would be a piece of cake if I could get the output of said URL directly into a PHP variable,in other words:

$JSONobj= whatever URL returns;
$tweets= (json_decode($JSONobj);

…I there a way to do this or something like it directly in PHP?

All help greatly appreciated!

Maybe with curl?

Is there any other method? I am not transferring a file, or replacing HTML… am merely getting a stringified object

Is the targeted “other page” actually a file on the same server then?


$json = file_get_contents('http://www.site.com/path/to/json/feed');

Don’t forget to add http(s):// so that file_get_contents knows you’re pulling a webpage’s source.

That’s the function i was trying to remember!!! Thanks Kduv.

cURL … also worked,as guido suggested, but ,for this, it seemed like overkill.

Maybe, but cURL always works, and file_get_contents can be locked down to not get external content on some web servers. So it is always good to recommend either :wink:

I found simplexml_load_file the other day after a search. It’s also another way. Use libxml_use_internal_errors() to supress the errror (although worth looking at. Saves going to w3c validator!)
If you’re used to a bit of XML it’s quite useful.

http://www.php.net/manual/en/function.simplexml-load-file.php