Introduction

Introduction – Introduction to HTTP_Request

Overview

With this package, one can easily perform HTTP request from within PHP scripts. It support GET/POST/HEAD/TRACE/PUT/DELETE, Basic authentication, Proxy, Proxy Authentication, SSL, file uploads etc.

Because of the above mentioned features HTTP_Request makes it possible to mimic big parts of web browsers such as the widely-known Mozilla browser in PHP applications. Possible application areas are:

  • Checking the validity of WWW links with the help of getResponseCode.
  • Grabbing remote web pages and parsing the result.
  • etc.

A few examples

Fetches yahoo.com and displays it

<?php
require_once "HTTP/Request.php";

$req =& new HTTP_Request("http://www.yahoo.com/");
if (!
PEAR::isError($req->sendRequest())) {
    echo 
$req->getResponseBody();
}
?>

Fetching two website in a row

In this example, two websites are fetched and displayed. To the first one a POST parameter is passed. The POST data stack is cleared before the second website is fetched.

<?php
require_once "HTTP/Request.php";

$req =& new HTTP_Request("http://www.php.net");
$req->setMethod(HTTP_REQUEST_METHOD_POST);
$req->addPostData("Foo""bar");
if (!
PEAR::isError($req->sendRequest())) {
     
$response1 $req->getResponseBody();
} else {
     
$response1 "";
}
     
$req->setMethod(HTTP_REQUEST_METHOD_GET);
$req->setURL("http://pear.php.net");
$req->clearPostData();
if (!
PEAR::isError($req->sendRequest())) {
     
$response2 $req->getResponseBody();
} else {
     
$response2 "";
}

echo 
$response1;
echo 
$response2;
?>