版本0.9,西蒙·威利森 2003年4月6日
HttpClient是HTTP协议的客户端类。它可用于与另一个网络服务器进行交互的PHP脚本。除了从服务器检索信息,HttpClient可以通过POST或GET与服务器交互。因此,它可以用作需要与在另一个站点上运行的应用程序通信的任何脚本的一部分。
官方网站:http://scripts.incutio.com/httpclient/index.php
本地地址:2017010622010593
官方下载地址:http://scripts.incutio.com/httpclient/HttpClient.tar.gz
特征
实现了有用的子集HTTP 1.0和1.1协议。
包括Cookie支持。
能够设置用户代理和引用字段。
可以自动处理重定向的网页。
可用于多个请求,由服务器发送的任何Cookie为每个附加请求重发。
支持gzip编码内容,这可以显着减少在事务中使用的带宽量。
面向对象,静态方法为简单请求提供了有用的快捷方式。
只能读取页面标题的功能 - 对于实现工具(如链接检查程序)非常有用。
支持文件上传。
手册英文
http://scripts.incutio.com/httpclient/manual.php
Only methods that are intended to be called from outside the script are described. For details of internal methods, Use The Source HttpClient一个PHP Web客户端类-爱程序
HttpClient($host, $port = 80)
Constructor. Takes the web server host (for example, 'scripts.incutio.com') and an optional port.
bool get($path, $data = false)
Executes a GET request for the specified path. If $data is specified, appends it to a query string as part of the get request. $data can be an array of key value pairs, in which case a matching query string will be constructed. Returns true on success and false on failure. If false, an error message describing the problem encountered can be accessed using the getError() method.
bool post($path, $data)
Executes a POST request to the specified path, sending the information from specified in $data. $data can be an array of key value pairs, in which case a matching post request will be constructed. Returns true on success and false on failure. If false, an error message describing the problem encountered can be accessed using the getError() method.
string getContent()
Returns the content of the HTTP response. This is usually an HTML document.
string getStatus()
Returns the status code of the response - 200 means OK, 404 means file not found, etc.
array getHeaders()
Returns the HTTP headers returned by the server as an associative array.
string getHeader($header)
Returns the specified response header, or false if it does not exist.
string getError()
Returns a string describing the most recent error.
string getRequestURL()
Returns the full URL that has been requested.
array getCookies()
Returns an array of cookies set by the server.
string quickGet($url)
Static method designed for running simple GET requests. Usage is:
$pageContent = HttpClient::quickGet($url);
Returns the empty string on failure.
string quickPost($url, $data)
Static method designed for running simple POST requests. Usage is:
$pageContent = HttpClient::quickPost($url, $data);
Returns the empty string on failure. See also post().
void setUserAgent($string)
Sets the user agent string to be used in the request. Default is "Incutio HttpClient v$version".
void setAuthorization($username, $password)
Sets the HTTP authorization username and password to be used in requests. Don't forget to unset this in subsequent requests to different servers.
void setCookies($array)
Sets the cookies to be sent in the request. Takes a 2D array of name value pairs.
void setUseGzip($boolean)
Specify if the client should request gzip encoded content from the server (saves bandwidth but can increase processor time). Default behaviour is TRUE.
void setPersistCookies($boolean)
Specify if the client should persist cookies between requests. Default behaviour is TRUE.
void setPersistReferers($boolean)
Specify if the client should use the URL of the previous request as the referral of a subsequent request. Default behaviour is TRUE.
void setHandleRedirects($boolean)
Specify if the client should automatically follow redirected requests. Default behaviour is TRUE.
void setMaxRedirects($int)
Set the maximum number of redirects allowed before the client quits (mainly to prevent infinite loops) Default is 5.
void setHeadersOnly($boolean)
If TRUE, the client only retrieves the headers from a page. This could be useful for implementing things like link checkers. Defaults to FALSE.
void setDebug($boolean)
Should the client run in debug mode? Default behaviour is FALSE.
void debug($message, $object = false)
This method is not intended to be called from outside the class; it is the method the class calls whenever there is debugging information available. $msg is a debugging message and $object is an optional object to be displayed (usually an array). Default behaviour is to display the message and the object in a red bordered div. If you wish debugging information to be handled in a different way you can do so by creating a new class that extends HttpClient and over-riding the debug() method in that class.
例子:
http://scripts.incutio.com/httpclient/examples.php
Grabbing an HTML page (static method)
$pageContents = HttpClient::quickGet('http://example.com/');
Posting a form and grabbing the response (static method)
$pageContents = HttpClient::quickPost('http://example.com/someForm', array(
'name' => 'Some Name',
'email' => 'email@example.com'
));
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
$client = new HttpClient('example.com');
if (!$client->get('/')) {
die('An error occurred: '.$client->getError());
}
$pageContents = $client->getContent();
A GET request with debugging turned on
$client = new HttpClient('example.com');
$client->setDebug(true);
if (!$client->get('/')) {
die('An error occurred: '.$client->getError());
}
$pageContents = $client->getContent();
A GET request demonstrating automatic redirection
$client = new HttpClient('www.amazon.com');
$client->setDebug(true);
if (!$client->get('/')) {
die('An error occurred: '.$client->getError());
}
$pageContents = $client->getContent();
Check to see if a page exists
$client = new HttpClient('example.com');
$client->setDebug(true);
if (!$client->get('/thispagedoesnotexist')) {
die('An error occurred: '.$client->getError());
}
if ($client->getStatus() == '404') {
echo 'Page does not exist!';
}
$pageContents = $client->getContent();
Fake the User Agent string
$client = new HttpClient('example.com');
$client->setDebug(true);
$client->setUserAgent('Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.3a) Gecko/20021207');
if (!$client->get('/')) {
die('An error occurred: '.$client->getError());
}
$pageContents = $client->getContent();
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.
$client = new HttpClient('example.com');
$client->post('/login.php', array(
'username' => 'Simon',
'password' => 'ducks'
));
if (!$client->get('/private.php')) {
die('An error occurred: '.$client->getError());
}
$pageContents = $client->getContent();
Using HTTP authorisation
Note that the class uses the American spelling 'authorization' to fit with the HTTP specification.
$client = new HttpClient('example.com');
$client->setAuthorization('Username', 'Password');
if (!$client->get('/')) {
die('An error occurred: '.$client->getError());
}
$pageContents = $client->getContent();
Print out the headers from a response
$client = new HttpClient('example.com');
if (!$client->get('/')) {
die('An error occurred: '.$client->getError());
}
print_r($client->getHeaders());
Setting the maximum number of redirects
$client = new HttpClient('www.amazon.com');
$client->setDebug(true);
$client->setMaxRedirects(3);
$client->get('/');
演示:
http://scripts.incutio.com/httpclient/demo.php
This demo (which may take a few seconds to load) attempts to retrieves the front page of Amazon.com and reports its size in bytes. Debug mode is switched on to show what the script is doing. This demonstrates the script automatically following redirections and maintaining cookies and referrers between sessions.
$client = new HttpClient('www.amazon.com');
$client->setDebug(true);
if (!$client->get('/')) {
echo '
Request failed!
';
} else {
echo '
Amazon home page is '.strlen($client->getContent()).' bytes.
';
}
HttpClient Debug: Request
GET / HTTP/1.0
Host: www.amazon.com
User-Agent: Incutio HttpClient v0.9
Accept: text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,image/jpeg,image/gif,*/*
Accept-encoding: gzip
Accept-language: en-us
HttpClient Debug: HTTP/1.1 301 Moved Permanently
HttpClient Debug: Received Headers
Array
(
[date] => Fri, 06 Jan 2017 14:04:52 GMT
[server] => Server
[location] => https://www.amazon.com/
[content-length] => 231
[keep-alive] => timeout=2, max=18
[connection] => Keep-Alive
[content-type] => text/html; charset=iso-8859-1
)
HttpClient Debug: Persisting referer: http://www.amazon.com/
HttpClient Debug: Request
GET / HTTP/1.0
Host: www.amazon.com
User-Agent: Incutio HttpClient v0.9
Accept: text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,image/jpeg,image/gif,*/*
Accept-encoding: gzip
Accept-language: en-us
Referer: http://www.amazon.com/
HttpClient Debug: HTTP/1.1 301 Moved Permanently
HttpClient Debug: Received Headers
Array
(
[date] => Fri, 06 Jan 2017 14:04:52 GMT
[server] => Server
[location] => https://www.amazon.com/
[content-length] => 231
[keep-alive] => timeout=2, max=18
[connection] => Keep-Alive
[content-type] => text/html; charset=iso-8859-1
)
HttpClient Debug: Persisting referer: http://www.amazon.com/
HttpClient Debug: Request
GET / HTTP/1.0
Host: www.amazon.com
User-Agent: Incutio HttpClient v0.9
Accept: text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,image/jpeg,image/gif,*/*
Accept-encoding: gzip
Accept-language: en-us
Referer: http://www.amazon.com/
HttpClient Debug: HTTP/1.1 301 Moved Permanently
HttpClient Debug: Received Headers
Array
(
[date] => Fri, 06 Jan 2017 14:04:53 GMT
[server] => Server
[location] => https://www.amazon.com/
[content-length] => 231
[keep-alive] => timeout=2, max=18
[connection] => Keep-Alive
[content-type] => text/html; charset=iso-8859-1
)
HttpClient Debug: Persisting referer: http://www.amazon.com/
HttpClient Debug: Request
GET / HTTP/1.0
Host: www.amazon.com
User-Agent: Incutio HttpClient v0.9
Accept: text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,image/jpeg,image/gif,*/*
Accept-encoding: gzip
Accept-language: en-us
Referer: http://www.amazon.com/
HttpClient Debug: HTTP/1.1 301 Moved Permanently
HttpClient Debug: Received Headers
Array
(
[date] => Fri, 06 Jan 2017 14:04:53 GMT
[server] => Server
[location] => https://www.amazon.com/
[content-length] => 231
[keep-alive] => timeout=2, max=20
[connection] => Keep-Alive
[content-type] => text/html; charset=iso-8859-1
)
HttpClient Debug: Persisting referer: http://www.amazon.com/
HttpClient Debug: Request
GET / HTTP/1.0
Host: www.amazon.com
User-Agent: Incutio HttpClient v0.9
Accept: text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,image/jpeg,image/gif,*/*
Accept-encoding: gzip
Accept-language: en-us
Referer: http://www.amazon.com/
HttpClient Debug: HTTP/1.1 301 Moved Permanently
HttpClient Debug: Received Headers
Array
(
[date] => Fri, 06 Jan 2017 14:04:53 GMT
[server] => Server
[location] => https://www.amazon.com/
[content-length] => 231
[keep-alive] => timeout=2, max=15
[connection] => Keep-Alive
[content-type] => text/html; charset=iso-8859-1
)
HttpClient Debug: Persisting referer: http://www.amazon.com/
HttpClient Debug: Number of redirects exceeded maximum (5)
Request failed!