http://

https://

http:// -- https://Acceso a URLs HTTP(s)

Descripción

Permite el acceso en modo solo lectura a ficheros accesibles mediante HTTP. Por omisión, se utiliza una solicitud HTTP 1.0 GET. Se envía un encabezado Host: con la solicitud, para gestionar los hosts virtuales basados en nombres. Si se ha configurado una versión de navegador con la opción user_agent en su archivo php.ini o mediante el contexto de flujo, también será incluida en su solicitud.

El flujo permite acceder al cuerpo (body) del recurso. Los encabezados se almacenan en la variable $http_response_header.

Si se necesita conocer la URL del recurso desde el cual proviene el documento (tras la ejecución de todas las redirecciones), deberá analizarse la serie de encabezados devueltos por el flujo.

La directiva from será utilizada para el encabezado From: si ha sido definida, y no será sobrescrita por los Opciones y parámetros de contexto.

Uso

  • http://example.com
  • http://example.com/fichero.php?var1=val1&var2=val2
  • http://user:password@example.com
  • https://example.com
  • https://example.com/fichero.php?var1=val1&var2=val2
  • https://user:password@example.com

Opciones

Resumen de la envoltura
Atributo Soportado
Restringido por allow_url_fopen
Permite la lectura
Permite la escritura No
Permite la adición No
Permite la lectura y escritura simultáneamente N/A
Soporte de la función stat() No
Soporte de la función unlink() No
Soporte de la función rename() No
Soporte de la función mkdir() No
Soporte de la función rmdir() No

Ejemplos

Ejemplo #1 Detecta la última URL tras redirecciones

<?php
$url
= 'http://www.example.com/redirecting_page.php';

$fp = fopen($url, 'r');

$meta_data = stream_get_meta_data($fp);
foreach (
$meta_data['wrapper_data'] as $response) {

/* ¿Hemos sido redirigidos? */
if (strtolower(substr($response, 0, 10)) == 'location: ') {

/* actualización de $url con la ruta tras redirección */
$url = substr($response, 10);
}

}

?>

Notas

Nota: HTTPS solo es soportado si la extensión openssl está activa.

Las conexiones HTTP son de solo lectura; la escritura de datos o la copia de ficheros a un recurso HTTP no son soportadas.

El envío de solicitudes POST y PUT, por ejemplo, puede realizarse mediante los contextos HTTP.

Ver también

add a note

User Contributed Notes 3 notes

up
7
dwalton at acm dot org
18 years ago
As it says on this page:

"The stream allows access to the body of the resource; the headers are stored in the $http_response_header variable. Since PHP 4.3.0, the headers are available using stream_get_meta_data()."

This one sentence is the only documentation I have found on the mysterious $http_response_header variable, and I'm afraid it's misleading. It implies that from 4.3.0 onward, stream_get_meta_data() ought to be used in favor of $http_response_header.

Don't be fooled! stream_get_meta_data() requires a stream reference, which makes it ONLY useful with fopen() and related functions. However, $http_response_header can be used to get the headers from the much simpler file_get_contents() and related functions, which makes it still very useful in 5.x.

Also note that even when file_get_contents() and friends fail due to a 4xx or 5xx error and return false, the headers are still available in $http_response_header.
up
5
Rainer Perske
9 years ago
Passing authentication information in the URL as in "https://user:password@example.com" works for HTTP "Basic" access authentication but not for HTTP "Digest" access authentication. You can use the cURL functions for servers requesting HTTP "Digest" access authentication.
up
1
NEA at AraTaraBul dot com
17 years ago
HTTP post function;

<?php
function post_it($datastream, $url) {

$url = preg_replace("@^http://@i", "", $url);
$host = substr($url, 0, strpos($url, "/"));
$uri = strstr($url, "/");

$reqbody = "";
foreach(
$datastream as $key=>$val) {
if (!empty(
$reqbody)) $reqbody.= "&";
$reqbody.= $key."=".urlencode($val);
}

$contentlength = strlen($reqbody);
$reqheader = "POST $uri HTTP/1.1\r\n".
"Host: $host\n". "User-Agent: PostIt\r\n".
"Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: $contentlength\r\n\r\n".
"$reqbody\r\n";

$socket = fsockopen($host, 80, $errno, $errstr);

if (!
$socket) {
$result["errno"] = $errno;
$result["errstr"] = $errstr;
return
$result;
}

fputs($socket, $reqheader);

while (!
feof($socket)) {
$result[] = fgets($socket, 4096);
}

fclose($socket);

return
$result;
}
?>
To Top