/    Sign up×
Community /Pin to ProfileBookmark

How to download source code of a website to hard drive

Hello.

How to save source code of a website to a hard drive using a programming language? I am thinking of a local file.htm with a form. I type there www.site.com into the form, I press a button there and then in the same folder I would like to have the source code of www.site.com saved named as x.htm.

Is there a way of doing it with Javascript? PHP?

Thank you.

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 06.2021 — If by "source code" you mean all the code on the web server used to generate the web pages, no -- unless it's your site and you want to create code on the site that allows you to do that.
Copy linkTweet thisAlerts:
@codewitchauthorJul 06.2021 — No, I mean just the html code of the site that receives the browser of the end user.

I do not mean the php code. I mean the html code only.
Copy linkTweet thisAlerts:
@NogDogJul 06.2021 — I'd probably use cURL functions, myself, though there may be a couple other ways.
[code=php]
<?php
$url = 'https://www.php.com/';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($ch)
echo "<pre>".htmlspecialchars($html)."</pre>";
[/code]
Copy linkTweet thisAlerts:
@codewitchauthorJul 06.2021 — Hello NogDog. Thank you for your reply. I tried the code that you posted and it does not work for me. Here is a little correction:

[CODE]
<?php
$url = 'https://www.php.com/';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($ch);
echo "<pre>".htmlspecialchars($html)."</pre>";
?>
[/CODE]


(there was a ; character missing after the $html = curl_exec($ch))

But even after that correction it does not work. Could we please repair the code?

Thank you.
Copy linkTweet thisAlerts:
@NogDogJul 06.2021 — Running locally from the command line, and I altered how I output the results to show the raw HTML and only the first 1000 characters):
<i>
</i>php &gt; $url = 'https://www.php.com/';
php &gt; $ch = curl_init($url);
php &gt; curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
php &gt; $html = curl_exec($ch);
php &gt; echo "&lt;pre&gt;".substr($html, 0, 1000)."&lt;/pre&gt;";
&lt;pre&gt;&lt;!doctype html&gt;
&lt;html lang="en-US" class="no-js"&gt;
&lt;head&gt;
&lt;!-- Google Tag Manager --&gt;
&lt;script&gt;(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&amp;l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-NCMVK5B');&lt;/script&gt;
&lt;!-- End Google Tag Manager --&gt;

<i> </i> &lt;!-- Global site tag (gtag.js) - Google Analytics --&gt;
<i> </i> &lt;script async src="https://www.googletagmanager.com/gtag/js?id=UA-117806314-1"&gt;&lt;/script&gt;
<i> </i> &lt;script&gt;
<i> </i> window.dataLayer = window.dataLayer || [];

<i> </i> function gtag() {
<i> </i> dataLayer.push(arguments);
<i> </i> }

<i> </i> gtag('js', new Date());

<i> </i> gtag('config', 'UA-117806314-1');
<i> </i> &lt;/script&gt;

<i> </i> &lt;meta charset = "UTF-8"&gt;
<i> </i> &lt;title&gt;Parents Helping Parents San Jose Special Ne&lt;/pre&gt;

If it won't work for you, maybe make sure you're seeing all warnings:
[code=php]
<?php
ini_set('display_errors', true); // set to false in production
error_log(E_ALL);

// rest of code...
[/code]
Copy linkTweet thisAlerts:
@codewitchauthorJul 06.2021 — Thank you for your posting NogDog. I do not understand what you mean by "running from a command line". I tried this code:

[CODE]
<!DOCTYPE html>
<html>
<body>

<?php
$url = 'https://www.php.com/';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($ch);
echo "<pre>".htmlspecialchars($html,0,1000)."</pre>";
?>

</body>
</html>
[/CODE]


And that displays nothing. Could you please explain the command line thing? Thank you.
Copy linkTweet thisAlerts:
@NogDogJul 06.2021 — > @codewitch#1633814 Could you please explain the command line thing?

You can run PHP interactively in a terminal window via:
php -a
Then you can enter commands a line at a time to try things out. (Use ctrl-C to exit if things get messed up and you want to start over. :) )

Per my debug suggestion, try changing your file to:
[code=php]
<!DOCTYPE html>
<html>
<body>

<?php
ini_set('display_errors', true);
error_reporting(E_ALL);

$url = 'https://www.php.com/';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($ch);
if(empty($html)) {
echo "<pre>cURL request failed:n".curl_error($ch)."</pre>";
} else {
echo "<pre>".htmlspecialchars($html,0,1000)."</pre>";
}
?>

</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@codewitchauthorJul 06.2021 — Ok. Thank you. Could we please stick to the php files without command line?

I tried the code you posted, I got this:

cURL request failed:

Could not resolve host: www.php.com
Copy linkTweet thisAlerts:
@NogDogJul 06.2021 — > @codewitch#1633818 cURL request failed:

> Could not resolve host: www.php.com


So how are you running this? If locally on your PC, is your PHP/web server configuration allowing access to external IPs?
Copy linkTweet thisAlerts:
@codewitchauthorJul 06.2021 — Currently I am in a library. Here is a computer with fedora operation system. I am not allowed to install here anything.

I use: https://www.w3schools.com/php/phptryit.asp?filename=tryphp_compiler
Copy linkTweet thisAlerts:
@NogDogJul 06.2021 — I suspect you'll need to figure out another place to test it, then, as apparently that site does not allow the PHP application to access external sites.
Copy linkTweet thisAlerts:
@codewitchauthorJul 06.2021 — Ok I understand. Thank you for your help.
×

Success!

Help @codewitch spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...