0

I'm looking for a simple script in which I can do something like this

$.getScript('fetcher.php?url=' + escape('http://www.google.com') + '&callback=console.log');

The response should be one really long line that looks like this:

console.log({responseText: '<!doctype html><html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"><title>Google</title><script>windo...'})

It shouldn't be more than 10 lines of code and there's no way it doesn't already exist.

I'm using php in XAMPP and am just using it to build a database so I don't need any frills included (no get vs post, no data included) just file_get_contents and $_GET. Of course I would still like to encoded the url

4
  • Do you limit the characters per line? If dont, I can write it in 1 line :D Commented Mar 31, 2011 at 2:39
  • @thaolt: How about a readable script, just not too intense of the options Commented Mar 31, 2011 at 2:41
  • This link describes this issue pretty well: bit.ly/eDLd4S Commented Mar 31, 2011 at 6:56
  • @SeanKinsey: I was asking because I assumed this already existed and didn't want to reinvent the wheel Commented Mar 31, 2011 at 12:06

2 Answers 2

1

How about this , updated

<?php
    // fetcher.php
    $url = $_GET['url'];
    $callback = $_GET['callback'];
    $read = file_get_contents($url);
    $read = addslashes(htmlspecialchars(str_replace("\n","\\n",$read)));
?>
<script>
    <?php echo $callback ?>({responseText: '<?php echo $read; ?>'});
</script>
Sign up to request clarification or add additional context in comments.

5 Comments

I want to preserve '\n'(linebreaks in the html) to become '\\n' (in the response) so that in the string it's a newline. Also I wan to be able to use escaped url
I didnt test this yet, did you try the code of dakis, Ill test it out
I didn't bother, I don't have curl
ok, ill try to help you out,str_replace("\n","\\n".. seems doesnt work
it run ok now, but I dont know if it fits your need
0

fetcher.php

<?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $_GET['url']);
    echo curl_exec($ch);
    curl_close($ch);
?>

javascript

$.get("fetcher.php", {url: "http://www.google.com/"}, function(response) {
    console.log(response);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.