HTTP Requests

The Solar_Http_Request class represents a standalone HTTP request (i.e., it's not an HTTP client, just a request). It uses adapters, so you can (in the future) change between a cURL, pecl_http, or sockets adapter -- but for now, the only adapter is the streams-based one.

It's a fluent class, which makes the code a little prettier at times. Here's a basic GET request that sends "?foo=bar" as the query string, and fetches a Solar_Http_Response object.

<?php
$request = Solar::factory('Solar_Http_Request');
$request->setUri('http://example.com/index.php')
        ->setContent(array('foo' => 'bar'));

$response = $request->fetch();
?>

(You could also set the query string in the URI if you wanted, but using setContent() does all the esaping for you.)

Here's the same thing as a POST:

<?php
$request = Solar::factory('Solar_Http_Request');

$request->setUri('http://example.com/index.php')
        ->setContent(array('foo' => 'bar'))
        ->setMethod('post');

$response = $request->fetch();
?>

And here's a request with a lot of customization:

<?php
$request = Solar::factory('Solar_Http_Request');

$request->setUri('http://example.com/index.php')
        ->setMethod('post')
        ->setHeader('X-Zim', 'Kneel before Zim!')
        ->setUserAgent('Irken Robot Gir')
        ->setBasicAuth('username', 'password')
        ->setContent(array('foo' => 'bar'));

$response = $request->fetch();
?>

Right now, Solar_Http_Request doesn't handle file uploads or the multipart/form-data content type, but that functionality is planned for a future release.

However, it does provide SSL support through PHP streams. You can make a secure request like this (note the use of https instead of http):

<?php
$request = Solar::factory('Solar_Http_Request');
$request->setUri('https://example.com/index.php')
        ->setContent(array('foo' => 'bar'));

$response = $request->fetch();
?>

If you need to, you can set various TLS and SSL parameters using these methods: