qwertymk
qwertymk

Reputation: 35284

Dead simple cross-domain php script

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

Upvotes: 0

Views: 1395

Answers (2)

thaolt
thaolt

Reputation: 485

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>

Upvotes: 1

Daniel
Daniel

Reputation: 704

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);
});

Upvotes: 0

Related Questions