Is There a JavaScript Way to Do File_Get_Contents()

Is there a JavaScript way to do file_get_contents()?

you could do

JS code:

$.post('phppage.php', { url: url }, function(data) {
document.getElementById('somediv').innerHTML = data;
});

PHP code:

$url = $_POST['url'];
echo file_get_contents($url);

That would get you the contents of the url.

How to use file_get_contents and json_decode in Javascript

Loading data via AJAX is asynchronous. Your first call ($.getJSON) is executed as soon as the page is loaded, but the callback function that you pass as a parameter, is executed only as soon as the underlying HTTP request is finished. This means that your program does not block to wait for the HTTP request; after calling $.getJSON(...) your program runs on, and the callback method is called at some time when the HTTP request finished.

You evaluate your data (in your second function) as soon as the page is loaded. But, since your data is loaded asynchronously, it is not yet loaded when the document is rendered and your function is executed.

The solution for your problem would be to move the code that evaluates your data into the callback function of $.getJSON(...):

$.getJSON('data.php', function(data) {
if (!data.error) {
// Process your data here!
} else {
alert(data.msg)
}
});

Insert Javascript in file_get_contents() php

you would need to use *_replace functions on the retrieved code, or use DOM classes to alter the code. There is no way to modify file_get_contents itself to add code

$script =<<<END
<script type="text/javascript" src="http://www.example.com/mysscript.js"></script>
</body>
END;
$html = file_get_contents($url);
$html = str_replace("</body>",$script,$html);

This assumes the content being grabbed has full html code, mainly includes the </body> tag

PHP file_get_contents of HTML file requests old resources

The problem is that the browser doesn't know that edit is actually edit/index.php. It thinks that this is a file in the root directory, so when parses script.js it looks for that in the root directory as well.

You should configure the webserver so that it performs a redirect from edit to edit/index.php, rather than just returning the contents of edit/index.php. This way, all relative URLs will be processed correctly. If you don't want to show the script name, you could just redirect to edit/ -- that's enough for it to know that this is a directory rather than a file.

Adding javascript to html file loaded by file_get_contents in php

<?php

$page = file_get_contents('http://example.com');
$doc = new DOMDocument();
$doc->loadHtml($page);

$head = $doc->getElementsByTagName("head")->item(0);

$js = $doc->createDocumentFragment();
$js->appendXml("<script> alert(1); </script>");
$head->appendChild($js);

echo $doc->saveHtml();

?>

This adds <script> alert(1); </script> to . Of course you can use "getElementById" and it be will exactly the same.

Hope this helps.

PHP `file_get_contents()` output pass to JavaScript and calculate length

Since no good answer, I would post my solution to my problem. The only way I could find was to hex-encode $url_data, pass it to JS, decode it and count the characters. For the pack() function I used the one ported in php.js.

...
$js_url_data = bin2hex($url_data);
...
if (!empty($js_url_data)) {
/* This is a good example when one is forced to use inline JS */
$script = <<<EOT
<script>
var url_data = "$js_url_data";
var url_data_len = pack('H*', url_data).length;
var node = document.createElement("li");
var textnode = document.createTextNode("JavaScript calculation page characters: " + url_data_len);
node.appendChild(textnode);
document.getElementById("resultList").appendChild(node);
</script>
EOT;
echo $script;
...


Related Topics



Leave a reply



Submit