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);
HTTP 和 HTTPS
PHP 3, PHP 4, PHP 5。自 PHP 4.3.0 起支持 https://
- http://example.com
- http://example.com/file.php?var1=val1&var2=val2
- http://user:password@example.com
- https://example.com
- https://example.com/file.php?var1=val1&var2=val2
- https://user:password@example.com
允许通过 HTTP 1.0 使用 HTTP GET 方法对文件/资源进行只读访问。Host: 报头也会通过请求发送出去以支持基于域名的虚拟主机。如果在 ini 文件或者流上下文配置了 user_agent 字符串,也会被包括在请求报头中。
当使用 SSL 时,Microsoft IIS 将违反协议不发送 close_notify 标记就关闭连接。PHP 将在到达数据尾端时报告 "SSL: Fatal Protocol Error"。要绕过此问题,应将 error_reporting 级别降低为不包括警告。PHP 4.3.7 及更高版本可以在当使用 https:// 封装协议打开流的时候检测出有此问题的 IIS 服务器并抑制警告。如果使用 fsockopen() 来创建一个 ssl:// 套接字,则需要自己检测并抑制警告信息。
自 PHP 4.0.5 起支持重定向。如果使用较早版本的 PHP 则需要在 URL 末尾包括一个斜线。如果一定要知道文档所在的资源的 url(在所有重定向被处理过之后),则需要处理从流返回的一系列响应报头信息。
<?php
$url = 'http://www.example.com/redirecting_page.php';
$fp = fopen($url, 'r');
/* 在 PHP 4.3.0 之前使用 $http_response_header
而不是 stream_get_meta_data() */
$meta_data = stream_get_meta_data($fp);
foreach($meta_data['wrapper_data'] as $response) {
/* 重定向到何处? */
if (substr(strtolower($response), 0, 18) == 'content-location: ') {
/* 把要重定向的链接赋值给 $url */
$url = substr($response, 18);
}
}
?>
流允许访问资源的正文,报头部分保存在 $http_response_header 变量中。自 PHP 4.3.0 起,可以用 stream_get_meta_data() 得到报头。
HTTP 连接是只读的,不能将数据写入或者拷贝文件到 HTTP 资源。
Note: 自 PHP 4.3.0 起开始支持 HTTPS,需要在编译时加入 OpenSSL 的支持。
| 属性 | 支持 |
|---|---|
| 受限于 allow_url_fopen | 是 |
| 允许读取 | 是 |
| 允许写入 | 否 |
| 允许附加 | 否 |
| 允许同时读写 | 无效 |
| 支持 stat() | 否 |
| 支持 unlink() | 否 |
| 支持 rename() | 否 |
| 支持 mkdir() | 否 |
| 支持 rmdir() | 否 |
| 名称 | 用法 | 默认值 |
|---|---|---|
| method | GET、POST 或任何其它被远程服务器支持的 HTTP 方式。 | GET |
| header | 请求中要发送的附加的头信息。此选项中的值将覆盖其它值(例如 User-agent:,Host: 和 Authentication:)。 | |
| user_agent | 在 User-Agent: 头信息中要发送的值。此值仅用于没有在上面 header 的上下文选项中指定 user-agent 时。 | php.ini 设置:user_agent |
| content | 头信息之后要发送的附加数据。典型用于 POST 或 PUT 请求。 | |
| proxy | 以 URI 格式指定的代理服务器(例如 tcp://proxy.example.com:5100)。HTTPS 代理(通过 HTTP 代理服务器)仅在 PHP 5.1.0 或更高版本可用。 | |
| request_fulluri | 设置为 TRUE 时,建立请求时整个 URI 将被使用。(即 GET http://www.example.com/path/to/file.html HTTP/1.0)。 而这是一个非标准请求格式,某些代理服务器需要它。 | FALSE |
| max_redirects | 连续重定向的最大值。取值为 1 或更小的值表示不跟随重定向。PHP 5.1.0 新加的。 | 20 |
Note: 底层的套接字流(socket stream)上下文选项 有可能通过底层传输(underlying transport)支持附加的上下文选项。对于 http:// 流,参考 tcp:// 传输的上下文选项。对于 https:// 流,参考 ssl:// 传输的上下文选项。
HTTP 和 HTTPS
26-Jun-2008 04:17
24-Oct-2007 03:27
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.
29-Jul-2007 04:06
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;
}
?>
28-Jun-2007 03:24
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');
?>
17-Nov-2006 12:18
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.
