Introduction

Introduction – Introduction to HTTP_Request2

Package Overview

HTTP_Request2 package provides an easy way for PHP applications to perform HTTP requests. It supports a large subset of Hypertext Transfer Protocol features, and can be used for the following:

  • Working with web services (numerous PEAR packages in Web Services category are using HTTP_Request2 under the hood);
  • Checking the validity of web links;
  • Grabbing and parsing remote web pages;
  • Automated form submission.

Basic Usage Пример

Performing a request with HTTP_Request2 consists of the following steps

  • Creating, configuring and populating an instance of HTTP_Request2 class. At the very least you should set request URL and maybe proxy parameters (if you are using proxy).
  • Calling send method of that instance. This will pass control to an Adapter that will send the request and read remote server's response. Request's progress may be monitored by Observers.
  • Processing the returned instance of HTTP_Request2_Response. An instance of HTTP_Request2_Exception can also be thrown by send() if response could not be received (completely or at all) or parsed.

Fetches and displays PEAR website homepage

<?php
require_once 'HTTP/Request2.php';

$request = new HTTP_Request2('http://pear.php.net/'HTTP_Request2::METHOD_GET);
try {
    
$response $request->send();
    if (
200 == $response->getStatus()) {
        echo 
$response->getBody();
    } else {
        echo 
'Unexpected HTTP status: ' $response->getStatus() . ' ' .
             
$response->getReasonPhrase();
    }
} catch (
HTTP_Request2_Exception $e) {
    echo 
'Error: ' $e->getMessage();
}
?>
    Поддержать сайт на родительском проекте КГБ