I have problem with variabl开发者_如何学Goe printing inside javascript.
variable htmlString printing not working: document.write(htmlString)
<?php $htmlString= htmlspecialchars(file_get_contents('http://google.com'));?>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<script type="text/javascript">
var htmlString="<?php echo $htmlString; ?>";
document.write(htmlString);
</script>
</body>
</html>
Edit:
Webpage source result: - Get all google.com inside htmlString the var not printed on the page(I cut the all content of htmlString because its very long)
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<script type="text/javascript">
var htmlString="<!doctype html><html><head><metahttp-equiv="content-type" cotring)";
document.write(htmlString);
</script>
</body>
</html>
Thanks
In order to pull remote pages with file_get_contents it requires fopen_wrappers to be enabled. If your host has this disabled and they allow cURL() I would go that route. cURL is also generally faster then file_get_contents, so that may be a deciding factor as well.
EDIT:
The problem you are having, particularly with google, is that it uses JS Code in the webpage. I just var_dump'ed the htmlString and it all displayed fine. But when putting it back into the JavaScript it went caput. The error that came back was an Unterminated String Literal (via Firefox's Error Console) on Line 8. Probably due to some single quotes etc. In my testing I tried htmlentities(), which worked and displayed the data to the browser. The section to change is:
$htmlString= htmlentities(file_get_contents('http://google.com'));
And it should work like you want it to.
精彩评论