/    Sign up×
Community /Pin to ProfileBookmark

Getting Source Code, Then Passing It Into A JavaScript Variable

I was told that I need to use a server-side language to get the source code of a web site. I figure since I have a little experience with PHP, and it’s a server-side language, I might as well ask here first. What I want to do is fairly simple to understand. I want to grab the source code of a website, store it in a JavaScript variable, and split it to get certain information. I already have the split part down, now I just need to get the source code into a JavaScript variable. I’ve tried this:

var sourcecode = <?PHP $sourcecode = htmlspecialchars(file_get_contents(‘url’));
echo $sourcecode ?>

But it didn’t work. I also tried making a PHP script outside of the JavaScript, and then calling the variable directly (I figured this wouldn’t work. It was kind of a last try kind of thing):

var sourcecode = $sourcecode

And, not surprisingly, it still didn’t work.

So, my question is, how can I grab the source code of any web page with PHP, and then pass that source code into a JavaScript variable as a string?

(sorry if all that is over-explained ^_^)

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@Phill_PaffordJul 25.2008 — does this work?

[code=php]
<?PHP
$url = "www.domain.com";
$sourcecode = htmlspecialchars(file_get_contents('$url'));
echo $sourcecode
?>
[/code]


if the PHP code works then your problem is passing the source code to javascript.

I have used AJAX in the past to do this, but not on such a large scale.

I have never worked with JSON but maybe this might also be an option for you.

Anyway wanted to join the post because it looks interesting, thanks
Copy linkTweet thisAlerts:
@NogDogJul 25.2008 — [code=php]
<?php

$html = file_get_contents('http://www.charles-reace.com');
$encoded = rawurlencode($html);
echo <<<END
<script type='text/javascript'>
var htmlText="$encoded";
document.write(unescape(htmlText));
</script>
END;

?>
[/code]
Copy linkTweet thisAlerts:
@SyCoJul 25.2008 — cURL works very well for this sort of thing.
Copy linkTweet thisAlerts:
@yomamacableguyauthorJul 25.2008 — SyCo, could you give an example code?

the first two did not work. ?
Copy linkTweet thisAlerts:
@NogDogJul 25.2008 — SyCo, could you give an example code?

the first two did not work. ?[/QUOTE]

I tested mine and it worked just fine for me. Can you show me how you tried to implement it?
Copy linkTweet thisAlerts:
@SyCoJul 25.2008 — Google for cURL tutorial. There's lots out there that will explain it better than me.

Also your issue is possibly wrappers

from the manual
A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename and List of Supported Protocols/Wrappers for a list of supported URL protocols.[/QUOTE]

RTFM file_get_contents()
Copy linkTweet thisAlerts:
@NogDogJul 25.2008 — Google for cURL tutorial. There's lots out there that will explain it better than me.

Also your issue is possibly wrappers

from the manual


RTFM file_get_contents()[/QUOTE]

Good point, since all I supplied was a bare-bones sample without any error-checking. If that's the case, replacing the file_get_contents() with a few lines of cURL functions.
Copy linkTweet thisAlerts:
@yomamacableguyauthorJul 25.2008 — NogDog, when i used your code, it just displayed the entire javascript as plain text on the page.

i'm going to try the cURL in a moment...
Copy linkTweet thisAlerts:
@NogDogJul 25.2008 — NogDog, when i used your code, it just displayed the entire javascript as plain text on the page.

i'm going to try the cURL in a moment...[/QUOTE]

Weird. Worked a champ for me. Did you save it as a .php file?
Copy linkTweet thisAlerts:
@yomamacableguyauthorJul 25.2008 — no. i guess that's probably why it didn't work. XD i put it straight into the html.
×

Success!

Help @yomamacableguy 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...