How to convert php file into html file - php

I'm a frontend developer and I'm facing with a problem.
Whenever I'm building a website, I'm using PHP to include the template files, so I get a redundant code.
But when I want to generate this file into an HTML file I open up the PHP file in the browser to copy/paste the code to an HTML file.
How can I make this process to be way much faster, or how could I avoid to do these things manually? Maybe there is a program to do this or something?

You can use a recursive wget.
Say your webserver runs on your localhost, you can run:
wget -r -k localhost
Be careful: wgetdoes not perform a search on which pages are available, it simply looks at links (the <a> tags) and will capture these as well. As long as everything is reachable from the index page (not necessarily on the index page), it will be downloaded.
wget is a linux program, but I guess there is a Windows application with the same name/options as well... As #rkbvkleef points out, it's part of the MinGW package.

Basically your php file (which runs on server, could be local server) contains or generates your HTML code to present on browser. You can simply write HTML code out of tags in a php file and it will work. Or if you want to generate some HTML based on some conditions you are checking inside php or using some variables in php then you can use echo function. It will display whatever string you echo on your webpage.
<?php
$name = "Murtaza";
echo("<h1>Hello ".$name."</h1>");
?>

Related

Can I use a PHP "index" page with AWS Amplify?

I am experimenting with using AWS. To be specific, I am following the tutorial at this link:
https://aws.amazon.com/getting-started/hands-on/build-web-app-s3-lambda-api-gateway-dynamodb/module-three/?e=gs2020&p=build-a-web-app-two
I have tried to add empty PHP tags to the start of the file and renamed the file to index.php instead of the index.HTML of the tutorial. I did a full sequence of refreshing the web app resources and deploying the app on the Amplify console. It did not work. I tried only using the HTML code on index.php and it still did not work. I put back the PHP code, added an echo statement echo "<h1>PHP Code Ran</h1>"; but renamed the file to index.html and it did render. Granted, there was an error in the text output. It also wrote the ending semi-colon and ending quotation, but it worked.
Is there any way for me to use a file named index.php as the home page of a web app using AWS amplify?
A PHP file isn't just an HTML file with a different name: you need to have a server somewhere that's running PHP which will look at the PHP code and run it.
If you're just uploading files to S3, that's not going to happen, the file is just going to be sent straight to the browser, regardless of what you call it and what you put in it.
Putting <?php echo "<h1>PHP Code Ran</h1>"; ?> into a file "worked" only in the sense that when you opened the page in the browser, you saw your browser's best attempt to interpret that as HTML. If you go to "View Source", you'll see that the file is exactly what you uploaded to S3, no PHP has run at all.
If you want to write a PHP application, you need to understand how to run PHP - most likely on an EC2 server, but it could also be in a Fargate container, or something even fancier like bref which lets you run PHP in a Lambda function.

Command to run html with php script

Can anyone tell here, what is the command to run html with php script. Normally in my linux terminal, if i want to run html, my command is
firefox <filename>.html
It works fine and display output in firefox browser. But some how, when I add php script, the browser doesn't show the output from php script. It show source code. I try the answer from the forum below, it doesn't work for me.
Using .htaccess to make all .html pages to run as .php files?
How to run a php script inside a html file?
So here, i want to ask
1) What is the step to run html with php script
2) what is the command to run?
3) The source code, need to save in .html or .php ?
Is there any way to run php in browser without using apache?
Rename file.html to file.php and check.
I think you write PHP and HTML in a single document, and you use only html extension.
You can't run php codes without install PHP package on your OS. your browser can't understand your php code without this package, So your browser show source code.
After that you can run php file as a page or put it into a html page by iframe html tag.

Return content of a local php file with params

I have a VPS with multiple domains. Now, one of those website is trying to get the output of another website which has a Piwik installation (open source analytics) on it.
Currently, I just use file_get_contents('https://domain.com?param=something'), which works fine, but the contents get send 'over the web', which I noticed when I changed some DNS settings and I couldn't get my content for a few hours.
Since everything is on the same server, is there a way I can access the local PHP-file (with the params), and return the content by using a file path such as /home/admin/domain/..? I know I can get the (literal) contents of a local file, but I can't pass the params and get the response I need, since the file doesn't get executed.
Is there a way to accomplish this?
If I've understood you correctly, then this command might help you:
php -f /path/to/file.php param=something > output.html
If you don't need to save the output of the command just omit the > output.html part.
There's a catch, though. By using https://domain.com?param=something you're passing a GET variable but with this command it's different; you have to extract the param from $argv (Explanation here). Therefore if you're not able to adapt your code then this won't help - but as far as I can tell you can.

how to run php file by double clicking from any folder like html files in windows xp?

Is there any trick to run .PHP files in windows XP from any folder by double clicking like HTML file ?
I use XAMPP but in this we need to put files ina special htdocs folder. I want to run file from any folder, desktop by double clicking.
After searching a lot I found a easy way to do this
PHPScriptNet – Great portable application for those who learning PHP. PHPScriptNet can run your PHP script anywhere in Windows machine without installing PHP or any webserver.
http://www.digitalcoding.com/free-software/webmaster-tools/PHPScriptNet-Portable-application-to-run-PHP-Script-from-windows.html
There is a significant difference in viewing HTML files vs. PHP files:
HTML files are static files interpreted by the browser. When you open them, the PATH to the HTML file is passes as an argument to the default browser which interprets and displays the file.
PHP files on the other hand are required to be interpreted by a PHP interpreter (XAMPP, in your case) before the resulting HTML is rendered by a browser. In this case, the local file PATH would have to be translated to the corresponding local URL, then sent to the browser.
Sample Solution
You could write a simple script that replaces '/var/www/' with 'http://localhost:8888/' (with a regular expression, for example) and passes that to the browser. Then, associate PHP files with your script.
PHP comes with a command line interface interpreter called php.exe (in Windows). This can be found in the root of your PHP installation directory.
You will need to associate .php files with this interpreter. That is, go into Tools -> Folder Options -> File types, and register php.exe -f "%1" as the application for the .php file type.
That said, it's not very typical to want to 'run' a PHP file by double-clicking it - not many people would use PHP the same way they'd use, for example, batch files or shell scripts - PHP is much better suited to generating web pages, ie using it in a web server. Chances are, running PHP outside of a web server is not what you really want to do here. For example, the PHP file will run within a command window and its output will be plain text, and if you want to see its output your script will need to pause or wait for input from standard input itself after terminating.
More information at Using PHP from the command line

How can I create a site in php and have it generate a static version?

For a particular project I have, no server side code is allowed. How can I create the web site in php (with includes, conditionals, etc) and then have that converted into a static html site that I can give to the client?
Update: Thanks to everyone who suggested wget. That's what I used. I should have specified that I was on a PC, so I grabbed the windows version from here: http://gnuwin32.sourceforge.net/packages/wget.htm.
If you have a Linux system available to you use wget:
wget -k -K -E -r -l 10 -p -N -F -nH http://website.com/
Options
-k : convert links to relative
-K : keep an original versions of files without the conversions made by wget
-E : rename html files to .html (if they don’t already have an htm(l) extension)
-r : recursive… of course we want to make a recursive copy
-l 10 : the maximum level of recursion. if you have a really big website you may need to put a higher number, but 10 levels should be enough.
-p : download all necessary files for each page (css, js, images)
-N : Turn on time-stamping.
-F : When input is read from a file, force it to be treated as an HTML file.
-nH : By default, wget put files in a directory named after the site’s hostname. This will disabled creating of those hostname directories and put everything in the current directory.
Source: Jean-Pascal Houde's weblog
build your site, then use a mirroring tool like wget or lwp-mirror to grab a static copy
I have done this in the past by adding:
ob_start();
In the top of the pages and then in the footer:
$page_html = ob_get_contents();
ob_end_clean();
file_put_contents($path_where_to_save_files . $_SERVER['PHP_SELF'], $page_html);
You might want to convert .php extensions to .html before baking the HTML into the files.
If you need to generate multiple pages with variables one quite easy option is to append the filename with md5sum of all GET variables, you just need to change them in the HTML too. So you can convert:
somepage.php?var1=hello&var2=hullo
to
somepage_e7537aacdbba8ad3ff309b3de1da69e1.html
ugly but works.
Sometimes you can use PHP to generate javascript to emulate some features, but that cannot be automated very easily.
Create the site as normal, then use spidering software to generate a HTML copy.
HTTrack is software I have used before.
One way to do this is to create the site in PHP as normal, and have a script actually grab the webpages (through HTTP - you can use wget or write another php script that just uses file() with URLs) and save them to the public website locations when you are "done". Then you can just run the script again when you decide to change the pages again. This method is quite useful when you have a slowly changing database and lots of traffic, as you can eliminate all SQL queries on the live site.
If you use modx it has a built in function to export static files.
If you have a number of pages, with all sorts of request variables and whatnot, probably one of the spidering tools the other commenters have mentioned (wget, lwp-mirror, etc) would be the easiest and most robust solution.
However, if the number of pages you need to get is low, or at least manageable, you've got a few options which don't require any third party tools (not that you should discount them JUSt because they are third party).
You can use php on the command line to get it to output directly into a file.
php myFile.php > myFile.html
Using this method could get painful (though you could put it all into a shell script), and it doesn't allow you to pass variables in the same way (eg: php myFile.php?abc=1 won't work).
You could use another PHP file as a "build" script which contains a list of all the URLs you want and then grabs them via file_get_contents() or file() and writes them to a local file. Using this method, you can also get it to check if the file has changed (md5_file() should work for that), so you'll know what to give your client, should they only want updates.
Further to #2, before you write the output to file, scan it for local urls and then add those to your list of files to download. While you're there, change those urls to link to what you'll eventually name your output so you have a functioning web at the end. Note of caution here - if this is sounding good, you could probably use one of the tools which already exist and do this for you.
Alternatively to wget you could use (Win|Web)HTTrack (Website) to grab the static page. HTTrack even corrects links to files and documents to match the static output.
You can use python or visual basic (or your choice) to create your static files all at once then upload them.
For a project with 11 million business listings in excel files I used VBA to extract the spreadsheet data into 11 mil small .php files, then zipped, ftp'd, unzipped.
https://contactlookup.us
Voila - a super fast business directory
I started with Jekyll, but after about half million entries the generator got bogged down. For 11 million it looked like it would finalize the build in about 2 months!
I do it on my own web site for certain pages that are guaranteed not to change -- I simply run a shell script that could be boiled to (warning: bash pseudocode):
find site_folder -name \*.static.php -print -exec Staticize {} \;
with Staticize being:
# This replaces .static.php with .html
TARGET_NAME="`dirname "$1"`/"`basename "$1" .static.php`".html
php "$1" > "$TARGET_NAME"
wget is probably the most complete method. If you don't have access to that, and you have a template based layout, you may want to look into using Savant 3. I recommend Savant 3 highly over other template systems like Smarty.
Savant is very light weight and uses PHP as the template language, not some proprietary sublanguage. The command you would want to look up is fetch(), which will "compile" your template and place it in a variable that you can output.
http://www.phpsavant.com/

Categories