Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg

PHP : Function Reference : HTTP



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:

Global Constants
Resource Types


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.


Be aware though, that some methods are not available with PHP v5.0.


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

Related Examples ( Source code ) » ref.http

Code Examples / Notes » ref.http


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)>
$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" onclick=setTimeout("location.href=''",0)>
Use this to one step back
<input type="button" value="Back" onclick=history.go(-1)>


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.


Note: the Classes are Only available in PHP5, the functions however work in both PHP4 and PHP5.

28-apr-2004 03:05

in reference to toashwinisidhu's and breaker's note, a more effective way would be to use meta-tag redirect, for example.
$url = ""; // 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.

henke dot andersson

If you want to make outgoing http connections with php, concider the curl extension.


Actually, if you want to redirect a user why let HTML or JavaScript do it? Simply do this:


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.)

Change Language

Follow Navioo On Twitter
.NET Functions
Apache-specific Functions
Alternative PHP Cache
Advanced PHP debugger
Array Functions
Aspell functions [deprecated]
BBCode Functions
BCMath Arbitrary Precision Mathematics Functions
PHP bytecode Compiler
Bzip2 Compression Functions
Calendar Functions
CCVS API Functions [deprecated]
Class/Object Functions
Classkit Functions
ClibPDF Functions [deprecated]
COM and .Net (Windows)
Crack Functions
Character Type Functions
Cybercash Payment Functions
Credit Mutuel CyberMUT functions
Cyrus IMAP administration Functions
Date and Time Functions
DB++ Functions
Database (dbm-style) Abstraction Layer Functions
dBase Functions
DBM Functions [deprecated]
dbx Functions
Direct IO Functions
Directory Functions
DOM Functions
DOM XML Functions
enchant Functions
Error Handling and Logging Functions
Exif Functions
Expect Functions
File Alteration Monitor Functions
Forms Data Format Functions
Fileinfo Functions
filePro Functions
Filesystem Functions
Filter Functions
Firebird/InterBase Functions
Firebird/Interbase Functions (PDO_FIREBIRD)
FriBiDi Functions
FrontBase Functions
FTP Functions
Function Handling Functions
GeoIP Functions
Gettext Functions
GMP Functions
gnupg Functions
Haru PDF Functions
hash Functions
Hyperwave Functions
Hyperwave API Functions
i18n Functions
IBM Functions (PDO_IBM)
iconv Functions
ID3 Functions
IIS Administration Functions
Image Functions
Imagick Image Library
Informix Functions
Informix Functions (PDO_INFORMIX)
Ingres II Functions
IRC Gateway Functions
PHP / Java Integration
JSON Functions
LDAP Functions
libxml Functions
Lotus Notes Functions
LZF Functions
Mail Functions
Mailparse Functions
Mathematical Functions
MaxDB PHP Extension
MCAL Functions
Mcrypt Encryption Functions
MCVE (Monetra) Payment Functions
Memcache Functions
Mhash Functions
Mimetype Functions
Ming functions for Flash
Miscellaneous Functions
mnoGoSearch Functions
Microsoft SQL Server Functions
Microsoft SQL Server and Sybase Functions (PDO_DBLIB)
Mohawk Software Session Handler Functions
mSQL Functions
Multibyte String Functions
muscat Functions
MySQL Functions
MySQL Functions (PDO_MYSQL)
MySQL Improved Extension
Ncurses Terminal Screen Control Functions
Network Functions
Newt Functions
NSAPI-specific Functions
Object Aggregation/Composition Functions
Object property and method call overloading
Oracle Functions
ODBC Functions (Unified)
ODBC and DB2 Functions (PDO_ODBC)
OpenAL Audio Bindings
OpenSSL Functions
Oracle Functions [deprecated]
Oracle Functions (PDO_OCI)
Output Control Functions
Ovrimos SQL Functions
Paradox File Access
Parsekit Functions
Process Control Functions
Regular Expression Functions (Perl-Compatible)
PDF Functions
PDO Functions
Phar archive stream and classes
PHP Options&Information
POSIX Functions
Regular Expression Functions (POSIX Extended)
PostgreSQL Functions
PostgreSQL Functions (PDO_PGSQL)
Printer Functions
Program Execution Functions
PostScript document creation
Pspell Functions
qtdom Functions
Rar Functions
GNU Readline
GNU Recode Functions
RPM Header Reading Functions
runkit Functions
SAM - Simple Asynchronous Messaging
Satellite CORBA client extension [deprecated]
SCA Functions
SDO Functions
SDO XML Data Access Service Functions
SDO Relational Data Access Service Functions
SESAM Database Functions
PostgreSQL Session Save Handler
Session Handling Functions
Shared Memory Functions
SimpleXML functions
SNMP Functions
SOAP Functions
Socket Functions
Standard PHP Library (SPL) Functions
SQLite Functions
SQLite Functions (PDO_SQLITE)
Secure Shell2 Functions
Statistics Functions
Stream Functions
String Functions
Subversion Functions
Shockwave Flash Functions
Swish Functions
Sybase Functions
TCP Wrappers Functions
Tidy Functions
Tokenizer Functions
Unicode Functions
URL Functions
Variable Handling Functions
Verisign Payflow Pro Functions
vpopmail Functions
W32api Functions
WDDX Functions
win32ps Functions
win32service Functions
xattr Functions
xdiff Functions
XML Parser Functions
XML-RPC Functions
XMLReader functions
XMLWriter Functions
XSL functions
XSLT Functions
YAZ Functions
YP/NIS Functions
Zip File Functions
Zlib Compression Functions
eXTReMe Tracker