支持的协议和封装协议
在线手册:中文  英文

http://

https://

http:// -- https://访问 HTTP(s) 网址

说明

允许通过 HTTP 1.0 的 GET方法,以只读访问文件或资源。 HTTP 请求会附带一个 Host: 头,用于兼容基于域名的虚拟主机。 如果在你的 php.ini 文件中或字节流上下文(context)配置了 user_agent 字符串,它也会被包含在请求之中。

数据流允许读取资源的 body,而 headers 则储存在了 $http_response_header 变量里。

如果需要知道文档资源来自哪个 URL(经过所有重定向的处理后), 需要处理数据流返回的系列响应报头(response headers)。

The from directive will be used for the From: header if set and not overwritten by the 上下文(Context)选项和参数.

可选项

可选项

封装协议概要
属性 支持
allow_url_fopen 限制 Yes
允许读取 Yes
允许写入 No
允许添加 No
允许同时读和写 N/A
支持 stat() No
支持 unlink() No
支持 rename() No
支持 mkdir() No
支持 rmdir() No

更新日志

版本 说明
4.3.7 检测 IIS 服务器避免 "SSL: Fatal Protocol Error" 错误。
4.3.0 添加 https://
4.0.5 增加了对重定向的支持。

范例

Example #1 检测重定向后最终的 URL

<?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) {

    
/* 我们是否被重定向了? */
    
if (strtolower(substr($response010)) == 'location: ') {

        
/* 更新我们被重定向后的 $url */
        
$url substr($response10);
    }

}

?>

Example #2 通过一个 HTTP 请求发送自定义的 HTTP 头

自定义头可以通过 上下文(context) 选项来发送。 同时也能够使用这种 hack: 通过利用 user_agent INI 设置的一个 处理的副作用来自定义头也是可能的。 设置 user_agent 为任意有效字符串(比如默认设置是 PHP/version) 并附带一对 回车/换行符(\r\n)以及其他任意额外的 HTTP 头。

<?php
ini_set
('user_agent'"PHP\r\nX-MyCustomHeader: Foo");

$fp fopen('http://www.example.com/index.php''r');
?>

以下结果的请求将被发送:

GET /index.php HTTP/1.0
Host: www.example.com
User-Agent: PHP
X-MyCustomHeader: Foo

注释

Note: openssl 扩展启用后才能够支持 HTTPS 协议。

HTTP 连接是只读的;还不支持对一个 HTTP 资源进行写数据或者复制文件。

比如发送 POSTPUT 请求, 可以在 HTTP Contexts 的支持下实现。

参见


支持的协议和封装协议
在线手册:中文  英文

用户评论:

gaver at telenet dot be (2012-06-02 11:38:33)

If you want to communicate between your server and a https server

<?php
$variable 
=$_GET['variable'];
$host "https://user:password@secure site"
$path "securescript.php"
$url=$host.$path;

$formdata = array ( "variable1" => "10000","variable2" => "02" "variable3" => "03" ); 

// get form data in a string
  
foreach($formdata AS $key => $val){ 
    
$poststring .= urlencode($key) . "=" urlencode($val) . "&"
  } 
// strip off trailing ampersand 
$poststring substr($poststring0, -1); 
// create a complete nice string
$urlcom$host.$path."?".$poststring;

// echo $urlcom; // if you want to debug your string

//and you can read out the data in one command... from a secure https server handling login and everything you need
 
$postdataget=file_get_contents($urlcom);
// and yes you can debug the responseheader to view if you have authorization
//  var_dump($http_response_header);

//and if you want to read the data, this is the response from the server
//  var_dump($postdataget);

?>

Nick Lewis (2008-06-26 04:17:14)

A note on how to deal with Cookies
To receive a cookie:
$httphandle = fopen($url,"r");
$meta = stream_get_meta_data($httphandle);
for ($j = 0; isset($meta['wrapper_data'][$j]); $j++) {
$httpline = $meta['wrapper_data'][$j];
@list($header,$parameters) = explode(";",$httpline,2);
@list($attr,$value) = explode(":",$header,2);
if (strtolower(trim($attr)) == "set-cookie") {
$cookie = trim($value);
break;
}
}
fclose($httphandle);
echo $cookie;
To send a cookie:
$user_agent = ini_get("user_agent");
ini_set("user_agent",$user_agent . "\r\nCookie: " . $cookie);
$httphandle = fopen($url,"r");
fclose($httphandle);
ini_set("user_agent",$user_agent);

spazdaq (2007-10-24 03:27:46)

just an FYI about digest authentication.
While one of the above http examples has the username and password info supplied with the url, this must only be for basic authentication. it does not appear to work for digest authentication. you have to handle the digest followup request on your own.

NEA at AraTaraBul dot com (2007-07-29 16:06:50)

HTTP post function;

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

$url preg_replace("@^http://@i"""$url);
$host substr($url0strpos($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($host80$errno$errstr);

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

fputs($socket$reqheader);

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

fclose($socket);

return 
$result;
}
?>

Sinured (2007-06-28 03:24:29)

If you want to send more than one custom header, just make header an array:

<?php
$default_opts 
= array(
    
'http' => array(
        
'user_agent' => 'Foobar',
        
'header' => array(
            
'X-Foo: Bar',
            
'X-Bar: Baz'
        
)
    )
);
stream_context_get_default($default_opts);
readfile('http://www.xhaus.com/headers');
?>

dwalton at acm dot org (2006-11-17 00:18:39)

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.

易百教程