note 41338 deleted from features.remote-files by nlopess
| From: | nlopess@php.net | Date: | Wed, 16 Mar 2005 15:15:47 +0000 |
| Subject: | note 41338 deleted from features.remote-files by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-86558@lists.php.net to get a copy of this message | ||
Note Submitter: caruccio at operamail dot com
----
Based on function submited from webmaster at elcurriculum dot com, I wrote this little function to
retrieve http too. It handle cookies, redirection, send and received headers, POST, GET and HEAD.
Here it is (caution, untested code):
<?php
function GetURL($url, $send_header, &$ret_header, $query = array(), $method = "GET")
{
$header = ''; // guarda os cabecalhos da resposta
$body = ''; // guarda o corpo da resposta
$buffer = ''; // buffer de leitura do socket
$ret_header = array();
$url_stuff = parse_url($url);
if (!isset($url_stuff["path"]))
$url_stuff["path"] = "/";
if (isset($url_stuff["query"]))
$_query = http_build_query($url_stuff["query"]);
if (sizeof($query))
$_query = http_build_query($query);
$fp = fsockopen ($url_stuff['host'], 80, $errno, $errstr, 30);
if ($fp) {
if ($method == "POST") {
$header = "$method $url_stuff[path] HTTP/1.1\r\n";
$header .= "Content-length: " . strlen($_query) . "\r\n";
$header .= "Content-type: application/x-www-form-urlencoded\r\n";
} else {
// montar os cabeçalhos de envio
if (isset($url_stuff['query']))
$header = "$method $url_stuff[path]?$url_stuff[query] HTTP/1.1\r\n";
else
$header = "$method $url_stuff[path] HTTP/1.1\r\n";
}
$header .= "Host: $url_stuff[host]\r\n";
foreach ($send_header as $hname => $hvalue)
$header .= "$hname: $hvalue\r\n";
$header .= "\r\n";
// enviar os cabecalhos
fputs($fp, $header);
if ($method == "POST")
fputs($fp, $_query);
// ler a resposta (cabecalhos + corpo)
while (!feof($fp))
$buffer .= fgets($fp, 4096);
// separar cabecalhos do corpo ( [cabechalho]\r\n\r\n[corpo] )
$ret = strpos($buffer, "\r\n\r\n", 0);
if ($ret == false) {
fclose($fp);
return -1;
}
$header = substr($buffer, 0, $ret); // cabecalho da respota
$body = substr($buffer, $ret + 4); // corpo da resposta
$header = str_replace("\r\n" , "\n", $header);
$array_h = explode("\n", $header);
foreach ($array_h as $line => $val) {
if (strpos($val, "HTTP/1") !== 0) {
$h = explode(":", $val);
$v = $h;
$h = $h[0];
array_shift($v); // shift left
$v = implode(":", $v); // reconstruct value
$ret_header[$h] = $v;
// save cookies
if (strtolower($h) === "set-cookie")
$send_header['Cookie'] = $v;
} else {
$ret_header['HTTP'] = $val;
$ret_header['HTTP_VERSION'] = substr($val, 5, 3);
$ret_header['HTTP_RETURN'] = substr($val, 9, 3);
$ret_header['HTTP_MESSAGE'] = substr($val, 12);
}
}
// Se o cabecalho possuir um campo "Location", segui-lo...
if (isset($ret_header['Location'])) {
$body = GetURL($ret_header['Location'], $send_header, $ret_header);
}
fclose ($fp);
}
return $body;
}
?>
Examples:
<?php
echo "TEST REDIRECT\n\n";
$url = "http://google.com"; // redirect to
www.google.com
$body = GetURL($url, $send_header, $ret_header);
echo '$ret_reader: ';
print_r($ret_header);
echo "\nBody\n=================\n";
echo $body;
echo "\n\nTEST QUERY + HEADERS\n\n";
$url = "http://www.google.com/search?q=php&start=0&start=0";
$send_header = array("Header_1" => "value123", "Header_2" =>
"value456");
$ret_header = array();
$query = array("q1" => "v1", "q2" => "v2");
$body = GetURL($url, $send_header, $ret_header, $query);
echo '$ret_reader: ';
print_r($ret_header);
echo "\nBody\n=================\n";
echo $body;
// test post (should return "501 Not Implemented")
echo "\n\nTEST POST + REDIRECT + QUERY\n\n";
$url = "http://google.com";
$body = GetURL($url, $send_header, $ret_header, $query, "POST");
echo '$ret_reader: ';
print_r($ret_header);
echo "\nBody\n=================\n";
echo $body;
?>