Note: the Classes are Only available in PHP5, the functions however work in both PHP4 and PHP5.
LVI. HTTP
Introduction
This HTTP extension aims to provide a convenient and powerful set of functionality for one of PHPs major applications.
It eases handling of HTTP URLs, dates, redirects, headers and messages, provides means for negotiation of clients preferred language and charset, as well as a convenient way to send any arbitrary data with caching and resuming capabilities.
It provides powerful request functionality, if built with CURL support. Parallel requests are available for PHP 5 and greater.
Additionally to the API reference in this manual you can find information about how to install and configure as well as which global constants are predefined in the following sections:
| Installation |
| Configuration |
| Global Constants |
| Resource Types |
Classes
The classes below are defined by this extension, and will only be available when the extension has either been compiled into PHP or dynamically loaded at runtime.
The HttpResponse class requires at least PHP v5.1.
Any other class is available as of PHP v5.0.
Note: Be aware though, that some methods are not available with PHP v5.0.
HttpMessage |
HttpQueryString |
HttpDeflateStream |
HttpInflateStream |
HttpRequest |
HttpRequestPool |
HttpResponse |
Functions
Built-in HTTP related functions previously listed on this page can be found in the networking category.
The following functions do not need the HTTP module to be present: header(), headers_list(), headers_sent(), setcookie() and setrawcookie().
| http_build_cookie() |
| http_date() |
| http_get_request_body_stream() |
| http_get_request_body() |
| http_get_request_headers() |
| http_match_etag() |
| http_match_modified() |
| http_match_request_header() |
| http_support() |
| http_redirect() |
| http_send_content_disposition() |
| http_send_content_type() |
| http_send_data() |
| http_send_file() |
| http_send_last_modified() |
| http_send_status() |
| http_send_stream() |
| http_throttle() |
Table of Contents
- Installation — Installing the HTTP extension
- Configuration — http module configuration directives
- Resources — Resources created by the HTTP extension
- Constants — predefined http module constants
- Request Options — options usable with the HttpRequest class and request functions
- HttpMessage — HTTP Message Class
- HttpMessage::__construct — HttpMessage constructor
- HttpMessage::factory — Create HttpMessage from string
- HttpMessage::fromEnv — Create HttpMessage from environment
- HttpMessage::fromString — Create HttpMessage from string
- HttpMessage::toString — Get string representation
- HttpMessage::toMessageTypeObject — Create HTTP object regarding message type
- HttpMessage::guessContentType — Guess content type
- HttpMessage::detach — Detach HttpMessage
- HttpMessage::prepend — Prepend message(s)
- HttpMessage::reverse — Reverse message chain
- HttpMessage::send — Send message
- HttpMessage::getParentMessage — Get parent message
- HttpMessage::getType — Get message type
- HttpMessage::setType — Set message type
- HttpMessage::getHttpVersion — Get HTTP version
- HttpMessage::setHttpVersion — Set HTTP version
- HttpMessage::getHeaders — Get message headers
- HttpMessage::getHeader — Get header
- HttpMessage::addHeaders — Add headers
- HttpMessage::setHeaders — Set headers
- HttpMessage::getBody — Get message body
- HttpMessage::setBody — Set message body
- HttpMessage::getRequestMethod — Get request method
- HttpMessage::setRequestMethod — Set request method
- HttpMessage::getRequestUrl — Get request URL
- HttpMessage::setRequestUrl — Set request URL
- HttpMessage::getResponseCode — Get response code
- HttpMessage::setResponseCode — Set response code
- HttpMessage::getResponseStatus — Get response status
- HttpMessage::setResponseStatus — Set response status
- HttpQueryString — HTTP Query String Class
- HttpQueryString::__construct — HttpQueryString constructor
- HttpQueryString::singleton — HttpQueryString singleton
- HttpQueryString::get — Get (part of) query string
- HttpQueryString::mod — Modifiy query string copy
- HttpQueryString::set — Set query string params
- HttpQueryString::toArray — Get query string as array
- HttpQueryString::toString — Get query string
- HttpQueryString::xlate — Change query strings charset
- HttpDeflateStream — HTTP Deflate Stream Class
- HttpDeflateStream::factory — HttpDeflateStream class factory
- HttpDeflateStream::__construct — HttpDeflateStream class constructor
- HttpDeflateStream::update — Update deflate stream
- HttpDeflateStream::flush — Flush deflate stream
- HttpDeflateStream::finish — Finalize deflate stream
- HttpInflateStream — HTTP Inflate Stream
- HttpInflateStream::factory — HttpInflateStream class factory
- HttpInflateStream::__construct — HttpInflateStream class constructor
- HttpInflateStream::update — Update inflate stream
- HttpInflateStream::flush — Flush inflate stream
- HttpInflateStream::finish — Finalize inflate stream
- HttpRequest — HTTP Request Class
- HttpRequest::addCookies — Add cookies
- HttpRequest::addHeaders — Add headers
- HttpRequest::addPostFields — Add post fields
- HttpRequest::addPostFile — Add post file
- HttpRequest::addPutData — Add put data
- HttpRequest::addQueryData — Add query data
- HttpRequest::addRawPostData — Add raw post data
- HttpRequest::addSslOptions — Add ssl options
- HttpRequest::clearHistory — Clear history
- HttpRequest::__construct — HttpRequest constructor
- HttpRequest::enableCookies — Enable cookies
- HttpRequest::getContentType — Get content type
- HttpRequest::getCookies — Get cookies
- HttpRequest::getHeaders — Get headers
- HttpRequest::getHistory — Get history
- HttpRequest::getMethod — Get method
- HttpRequest::getOptions — Get options
- HttpRequest::getPostFields — Get post fields
- HttpRequest::getPostFiles — Get post files
- HttpRequest::getPutData — Get put data
- HttpRequest::getPutFile — Get put file
- HttpRequest::getQueryData — Get query data
- HttpRequest::getRawPostData — Get raw post data
- HttpRequest::getRawRequestMessage — Get raw request message
- HttpRequest::getRawResponseMessage — Get raw response message
- HttpRequest::getRequestMessage — Get request message
- HttpRequest::getResponseBody — Get response body
- HttpRequest::getResponseCode — Get response code
- HttpRequest::getResponseCookies — Get response cookie(s)
- HttpRequest::getResponseData — Get response data
- HttpRequest::getResponseHeader — Get response header(s)
- HttpRequest::getResponseInfo — Get response info
- HttpRequest::getResponseMessage — Get response message
- HttpRequest::getResponseStatus — Get response status
- HttpRequest::getSslOptions — Get ssl options
- HttpRequest::getUrl — Get url
- HttpRequest::resetCookies — Reset cookies
- HttpRequest::send — Send request
- HttpRequest::setContentType — Set content type
- HttpRequest::setCookies — Set cookies
- HttpRequest::setHeaders — Set headers
- HttpRequest::setMethod — Set method
- HttpRequest::setOptions — Set options
- HttpRequest::setPostFields — Set post fields
- HttpRequest::setPostFiles — Set post files
- HttpRequest::setPutData — Set put data
- HttpRequest::setPutFile — Set put file
- HttpRequest::setQueryData — Set query data
- HttpRequest::setRawPostData — Set raw post data
- HttpRequest::setSslOptions — Set ssl options
- HttpRequest::setUrl — Set URL
- HttpRequestPool — HTTP Request Pool Class
- HttpRequestPool::attach — Attach HttpRequest
- HttpRequestPool::__construct — HttpRequestPool constructor
- HttpRequestPool::__destruct — HttpRequestPool destructor
- HttpRequestPool::detach — Detach HttpRequest
- HttpRequestPool::getAttachedRequests — Get attached requests
- HttpRequestPool::getFinishedRequests — Get finished requests
- HttpRequestPool::reset — Reset request pool
- HttpRequestPool::send — Send all requests
- HttpRequestPool::socketPerform — Perform socket actions
- HttpRequestPool::socketSelect — Perform socket select
- HttpResponse — HTTP Response Class
- HttpResponse::capture — Capture script output
- HttpResponse::getBufferSize — Get buffer size
- HttpResponse::getCacheControl — Get cache control
- HttpResponse::getCache — Get cache
- HttpResponse::getContentDisposition — Get content disposition
- HttpResponse::getContentType — Get content type
- HttpResponse::getData — Get data
- HttpResponse::getETag — Get ETag
- HttpResponse::getFile — Get file
- HttpResponse::getGzip — Get gzip
- HttpResponse::getHeader — Get header
- HttpResponse::getLastModified — Get last modified
- HttpResponse::getStream — Get Stream
- HttpResponse::getThrottleDelay — Get throttle delay
- HttpResponse::getRequestBody — Get request body
- HttpResponse::getRequestBodyStream — Get request body stream
- HttpResponse::getRequestHeaders — Get request headers
- HttpResponse::guessContentType — Guess content type
- HttpResponse::redirect — Redirect
- HttpResponse::send — Send response
- HttpResponse::setBufferSize — Set buffer size
- HttpResponse::setCacheControl — Set cache control
- HttpResponse::setCache — Set cache
- HttpResponse::setContentDisposition — Set content disposition
- HttpResponse::setContentType — Set content type
- HttpResponse::setData — Set data
- HttpResponse::setETag — Set ETag
- HttpResponse::setFile — Set file
- HttpResponse::setGzip — Set gzip
- HttpResponse::setHeader — Set header
- HttpResponse::setLastModified — Set last modified
- HttpResponse::setStream — Set stream
- HttpResponse::setThrottleDelay — Set throttle delay
- HttpResponse::status — Send HTTP response status
- http_cache_etag — Caching by ETag
- http_cache_last_modified — Caching by last modification
- http_chunked_decode — Decode chunked-encoded data
- http_deflate — Deflate data
- http_inflate — Inflate data
- http_get_request_body_stream — Get request body as stream
- http_get_request_body — Get request body as string
- http_get_request_headers — Get request headers as array
- http_date — Compose HTTP RFC compliant date
- http_support — Check built-in HTTP support
- http_match_etag — Match ETag
- http_match_modified — Match last modification
- http_match_request_header — Match any header
- http_build_cookie — Build cookie string
- http_negotiate_charset — Negotiate clients preferred character set
- http_negotiate_content_type — Negotiate clients preferred content type
- http_negotiate_language — Negotiate clients preferred language
- ob_deflatehandler — Deflate output handler
- ob_etaghandler — ETag output handler
- ob_inflatehandler — Inflate output handler
- http_parse_cookie — Parse HTTP cookie
- http_parse_headers — Parse HTTP headers
- http_parse_message — Parse HTTP messages
- http_parse_params — Parse parameter list
- http_persistent_handles_count — Stat persistent handles
- http_persistent_handles_ident — Get/set ident of persistent handles
- http_persistent_handles_clean — Clean up persistent handles
- http_get — Perform GET request
- http_head — Perform HEAD request
- http_post_data — Perform POST request with pre-encoded data
- http_post_fields — Perform POST request with data to be encoded
- http_put_data — Perform PUT request with data
- http_put_file — Perform PUT request with file
- http_put_stream — Perform PUT request with stream
- http_request_method_exists — Check whether request method exists
- http_request_method_name — Get request method name
- http_request_method_register — Register request method
- http_request_method_unregister — Unregister request method
- http_request — Perform custom request
- http_request_body_encode — Encode request body
- http_redirect — Issue HTTP redirect
- http_send_content_disposition — Send Content-Disposition
- http_send_content_type — Send Content-Type
- http_send_data — Send arbitrary data
- http_send_file — Send file
- http_send_last_modified — Send Last-Modified
- http_send_status — Send HTTP response status
- http_send_stream — Send stream
- http_throttle — HTTP throttling
- http_build_str — Build query string
- http_build_url — Build an URL
HTTP
11-Aug-2006 01:19
15-Jan-2006 06:01
If you want to make outgoing http connections with php, concider the curl extension.
01-Dec-2005 12:57
Actually, if you want to redirect a user why let HTML or JavaScript do it? Simply do this:
header("Location: http://www.example.com/");
08-Jul-2004 03:39
Regarding what the guy before said. We've experienced problems where certain firewalls have encrypted the HTTP_REFERER meaning that it doesnt always contain the place you've come from.
Better to track where the user has come from either in a form post or in the url.
in reference to toashwinisidhu's and breaker's note, a more effective way would be to use meta-tag redirect, for example.
<?php
$url = "http://somesite.com/index.php"; // target of the redirect
$delay = "3"; // 3 second delay
echo '<meta http-equiv="refresh" content="'.$delay.';url='.$url.'">';
?>
The meta goes in the head of the HTML.
This method does not require javascript and is supported by most browsers and is rarely, if ever, filterd out.
21-Apr-2004 07:55
The method given below may not sometimes work.
The following method has always worked with me:
just put the following 3 lines in your PHP code
?>
<body onload=setTimeout("location.href='$url'",$sec)>
<?PHP
-------?>
$sec is the time in second after which the browser would automatically go to the url. Set it to 0 if you do not want to give any time.
You can use this function on the events of various html/form objects (eg.-onclick for button).eg.
<input type=button value="Go to Php.net" onclick=setTimeout("location.href='php.net'",0)>
Use this to one step back
<input type="button" value="Back" onclick=history.go(-1)>
05-Jan-2001 01:37
$HTTP_RAW_POST_DATA --
You'll usually access variables from forms sent via POST method by just accessing the associated PHP global variable.
However, if your POST data is not URI encoded (i.e., custom application that's not form-based) PHP won't parse the data into nice variables for you. You will need to use $HTTP_RAW_POST_DATA to access the raw data directly. (This should return a copy of the data given to the PHP process on STDIN; note that you wan't be able to open STDIN and read it yourself because PHP already did so itself.)