转自http://www.wappler.eu/php-httpclient/
PHP-HttpClient based on Simon Willisons HttpClient Version 0.9.
HttpClient is a client class for the HTTP protocol. It can be used to interact with another web server from within a PHP script. As well as retrieving information from a server, HttpClient can interact with a server via POST or GET. It can therefore be used as part of any script that needs to communicate with an application running on another site.
Zu Deutsch: PHP-HttpClient ist eine Klasse, mit der man einen Browser in PHP simulieren kann. So kann man Inhalte von anderen Webseiten holen und in die eigene Webseite einbinden. Ich musste die Version von Simon Willison ein wenig erweitern, um mehrere Dateien per POST übertragen zu können.
Features
- HTTP 1.0
- Supports Cookies
- Custom User-Agent
- Can automatically handle redirections
- GET, POST and multiple File-Uploads via POST
- Debugging
- Supports password-protected sites
Download
Download HttpClient.class.php
Usage
Grabbing an HTML page (static method)
Posting a form and grabbing the response (static method)
The static methods are easy to use, but seriously limit the functionality of the class as you cannot access returned headers or use facilities such as cookies or authentication.
A simple GET request using the class
A GET request with debugging turned on
A GET request demonstrating automatic redirection
Check to see if a page exists
Fake the User Agent string
Log in to a site via a login form, then request a page
In this example, it is assumed that correctly logging in will result in the server sending a sesssion cookie of some sort. This cookie is resent by the HttpClient class automatically, so a request to a page that requires users to be logged in will now work.
Using HTTP authorisation
Note that the class uses the American spelling ‘authorization’ to fit with the HTTP specification.
Print out the headers from a response
Setting the maximum number of redirects