欢迎访问我的生活博客 http://jansfer.com

HttpClient - a PHP Web Client Class

上一篇 / 下一篇  2006-11-30 15:10:29

  • 文件版本: V1.0
  • 开发商: 本站原创
  • 文件来源: 本地
  • 界面语言: 简体中文
  • 授权方式: 免费
  • 运行平台: Win9X/Win2000/WinXP
示例:

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

$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('/');

本地下载

TAG: PHP HttpClient

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

我的栏目

日历

« 2008-11-17  
      1
2345678
9101112131415
16171819202122
23242526272829
30      

数据统计

  • 访问量: 129088
  • 日志数: 430
  • 图片数: 17
  • 商品数: 1
  • 文件数: 19
  • 书签数: 12
  • 建立时间: 2006-05-18
  • 更新时间: 2008-09-23

RSS订阅

Open Toolbar