PHP and javascript snippets you can copy and paste.

Wednesday, March 21, 2007

net_get_remoteFile($path,$method='GET',$timeout=30)

/**
* net-get-RemoteFile()
*
* Returns a remote fle.
* @param string $path - path to the remote file.
* @param string $method -method to use," get="" or="" post="">
* @param int $timeout - seconds before timeout, defaults to 30.
* @return string.
*/

function net_get_remoteFile($path,$method='GET',$timeout=30){

$parts = parse_url($path);
$params = isset($parts['query'])?$parts['query']:'';

$fp = fsockopen(isset($parts['host'])?$parts['host']:$parts['path'], 80, $errno, $errstr,$timeout);
if (!$fp) {
echo "$errstr ($errno)\n";
}
else {
$out = strtoupper($method). " " . $parts['path'] . " HTTP/1.1\r\n";
$out .= "Accept: */*\n";
$out .= "Host: " . $parts['host'] . "\r\n";
if(strtoupper($method) == "POST" ) {
$strlength = strlen($params);
$out .= "Content-type: application/x-www-form-urlencoded\n";
$out .= "Content-length: ".$strlength."\n\n";
$out .= $params."\n";
}

$out .= "Connection: Close\r\n\r\n";

fwrite($fp, $out);
$contents = '';

while (!feof($fp)) {
$contents .= fgets($fp, 1024);
}
fclose($fp);
}

return $contents;

}

No comments: