Team LiB
Previous Section Next Section

HTTP Client Methods

In the world of HTTP, a client method is the request sent from the Web client, either a browser or a PHP script of yours, to the HTTP server. The method tells the Web server what action the client wants to perform. There are three main types of requests:

  • Get Requests When you want only to retrieve information from an HTTP source, you do this with a GET method. Because what you are retrieving from is a URL, you could be either getting the information inside a file (of any type) or actually executing a program on the Web server. The beauty of HTTP is that GET requests make program execution as simple as retrieving a file.

  • Post Requests When you want to send information back to the Web server from the client, you use a POST request. This is generally used when you're sending the contents of Web forms back to a Web server.

  • Head Requests When you want to retrieve information about the URL being requested but not the information within the URL, you use a HEAD request. Think of a HEAD request as similar to the PHP stat() function, which returns information about a file. Although the information returned is different, the concept is the same.

The following are examples for using GET and POST requests using the HTTP::Client library.

<?php
 require_once "HttpClient.class.php";
 $client = new HttpClient('feedster.com');
 if (!$client->get('/status.php')) {
  die('An error occurred: '.$client->getError());
 }
 $pageContents = $client->getContent();
?>
<?php
 $pageContents = HttpClient::quickPost('http://Feedster.com/search.php', array(
  'q' => 'RSS',
  'sort' => 'date'
));
?>

Although GET, POST, and HEAD are the primary client methods you use, there are other methods as well: CONNECT, DELETE, LINK, OPTIONS, PATCH, PUT, TRACE, and UNLINK.

    Team LiB
    Previous Section Next Section