Source: Transclusion PHP-->Javascript
<?php
/*
Author: Tim Langeman www.openpolitics.com/tim 2004-24-01
Explanaition: http://www.openpolitics.com/2005/01/25/transquotation_with_javascript.html
This PHP Script fetches the contents of a remote URL and
Outputs the results to the browser in Javascript Output format:
document.write("contents");
The URL specified in the querystring must not include
characters that will confuse it:
"?" should be replaced with "quote;"
"&" should be replaced with "amp;"
If you don't escape these characters, the script with think
that the following text belongs to a new paramater
If anyone has a better way of escaping these characters, let me know
*/
$myURL = $_GET['url']; //get the URL from the querystring
$search = array("?", "&"); //search for this
$replace = array("quote;", "amp;"); //replace with this
$myURL = str_replace($replace, $search, $myURL);
if (strlen( $myURL ) > 0 ) {
//Call the function: retreive data from the remote web server
$quote = fetchURL( $myURL, 1, 0);
//Javascript Output is html, so escape any non-html characters
$quote = htmlentities($quote, ENT_QUOTES);
//Output, using javascript output syntax
echo("document.write(\"" . $quote . "\");"); }
else {
$errMsg = "No URL specified in querystring<br />";
$errMwg .= "See <a href=\"http://www.openpolitics.com/transquote/source.php\">";
$errMsg .= "Source Documentation</a> for details.";
echo("document.write(\" $errMsg \");");
}
//************** End Main Program: Begin Functions **************
function fetchURL( $url ) {
//Open a connection on port 80 to the remote URL
//Download the data and return the results
//Taken from: http://www.php.net/manual/en/function.file.php
$url_parsed = parse_url($url);
$host = $url_parsed["host"];
$port = $url_parsed["port"];
if ($port==0)
$port = 80;
$path = $url_parsed["path"];
if ($url_parsed["query"] != "")
$path .= "?".$url_parsed["query"];
$out = "GET $path HTTP/1.0\r\nHost: $host\r\n\r\n";
//open a connection with a timeout of 30 seconds
$fp = fsockopen($host, $port, $errno, $errstr, 30);
fwrite($fp, $out);
$body = false;
while (!feof($fp)) {
$s = fgets($fp, 1024);
if ( $body )
$in .= $s;
if ( $s == "\r\n")
$body = true;
}
fclose($fp);
return $in;
}
//******************** End Functions *********************
?>