Solved

Using file_put_contents() to insert dynamic content into HTML file. Is it possible?

Posted on 2013-11-18
15
707 Views
Last Modified: 2013-11-19
Experts,

I am trying to build a page dynamically using the file_put_contents() function. I am wondering if this is possible?

Example:

My file (our_employees.php) builds a dynamic page by querying several data sources and returning the results.

Is it possible to use the file_put_contents() function to create a new page which has the rendered results of the our_employees.php page as the content?

Wrong syntax but, trying to do:

file_put_contents("our_employees.html", RENDERED RESULTS OF our_employees.php);
0
Comment
Question by:evibesmusic
  • 6
  • 5
  • 4
15 Comments
 
LVL 58

Assisted Solution

by:Gary
Gary earned 250 total points
ID: 39658028
file_put_contents("our_employees.html", file_get_contents("http://www.site.com/myfile.php"));

or for readability

$getfile=file_get_contents("http://www.site.com/myfile.php");
file_put_contents("our_employees.html", $getfile);
0
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 250 total points
ID: 39658030
I wouldn't do it that way.  I would use the 'include' function in PHP to 'include' the new content in the page.  http://us3.php.net/manual/en/function.include.php
0
 
LVL 58

Expert Comment

by:Gary
ID: 39658032
Dave is right, unless there is some reason not to.
Hmmm, are you just trying to create a static html version of the files.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39658047
After re-reading your question, it appears that you are just making a second copy of 'our_employees.php'.  Why?  Why not just use 'our_employees.php' each time so it will be current?
0
 

Author Comment

by:evibesmusic
ID: 39658059
@All:

Sorry for the difficulty in articulating what I need.

I think I've answered my own question using the internal buffering provided by the ob_start(); and ob_get_contents(); functions.

I basically wanted to capture the rendered HTML produced by our_employees.php, and copy the HTML into a file called our_employees.html. The following seems to be producing and saving what I need.

Can either of you think of why I wouldn't want to do it this way? Pitfalls?

I intend to ultimately have this process run once per day on our local server in order to keep the content of our_employees.html up to date.

<?php
//START INTERNAL CACHING
ob_start();
//INCLUDE THE PAGE WE WANT RENDERED
include(our_doctors.php);
//GET THE CONTENTS OF THE INTERNAL CACHE AND STORE IN VARIABLE
$content = ob_get_contents();
//CREATE AND/OR RE-WRITE THE DESIRED PAGE
//ADD CACHED CONTENT TO BODY OF PAGE
file_put_contents("our_doctors.html",$content);
?>
0
 

Author Comment

by:evibesmusic
ID: 39658063
@DaveBaldwin:

Re: "After re-reading your question, it appears that you are just making a second copy of 'our_employees.php'.  Why?  Why not just use 'our_employees.php' each time so it will be current? "

Good question.

I am attempting to come up with a creative solution here.

The server which houses the our_employees.html file is unable to connect to several databases. Otherwise, I would as you suggest just use the PHP version of the file which would then build the page (and its contents on the fly).

The thought was to have the script I shared above, run once per day. Therefore, the our_employee.html file will be updated at least daily if I can't have a constant connection to the data sources needed.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39658066
Still a bit confused - so our_employees.html is on a different server from our_employees.php.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:evibesmusic
ID: 39658071
@GaryC123:

Correct.

After getting the cached HTML from our_employees.php, and creating the our_employees.html file, I will use PHP to upload our_employees.html to the other server.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39658076
You could always use the same example I listed above to get it from the other server.
file_get_contents (or curl) can be used for remote files.
I don't see includes being of any use here in this case.
0
 

Author Comment

by:evibesmusic
ID: 39658078
@All:

Thanks for the help. I will try using the alternate methods both of you have suggested.

Happy Holidays from California!

:)
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39658109
If PHP and allow_url_fopen are enabled on the second server, you can 'include' the HTML from 'our_employees.php' on the first server in a file on the second server by putting the 'http://' in the file name.

include 'http://firstserver/our_employees.php'

or use the IP address.

include 'http://10.10.10.10/our_employees.php'

That will cause the 'our_employees.php' to run on the first server and return the results in the page on the second server.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39658124
allow_url_fopen = dangerous.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39658628
Why do you say that it's dangerous?  I thought about it for a while and I can't see how that is true.  Half of the web hosts I use have it On and half have it Off.
0
 

Author Comment

by:evibesmusic
ID: 39659952
@DaveBaldwin:

One of the servers is public, the other is not. I won't be able to use your suggestion.

I am however interested in your conversation with @GaryC123 regarding the use of the allow_url_fopen() command.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39660100
In itself it is not dangerous, but could be utilised for cross site scripting if your site/server was hacked.
You would find most shared hosting will not allow it.
In your situation there is no reason to use it because you have other avenues that can do the same thing
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now