{
    "href": "/post/2007/07/17/brief-intro-to-solar-http-request-and-response/",
    "relId": "2007/07/17/brief-intro-to-solar-http-request-and-response",
    "title": "Brief Intro to Solar_Http_Request and Response",
    "author": "pmjones",
    "markup": "html",
    "tags": [
        {
            "href": "/tag/php/",
            "relId": "php",
            "title": "PHP",
            "author": null,
            "created": null,
            "updated": [],
            "markup": "markdown"
        },
        {
            "href": "/tag/solar/",
            "relId": "solar",
            "title": "Solar",
            "author": null,
            "created": null,
            "updated": [],
            "markup": "markdown"
        }
    ],
    "created": "2007-07-17 12:00:08 UTC",
    "updated": [
        "2007-07-17 12:00:08 UTC"
    ],
    "html": "<p>As I noted in an <a href=\"http://paul-m-jones.com/blog/?p=249\">earlier post</a>, <a href=\"http://solarphp.com\">Solar</a> how has classes to represent simple HTTP requests and responses.</p>\n<h3>Solar_Http_Request</h3>\n<p>The <a href=\"http://solarphp.com/class/Solar_Http_Request_Adapter\">Solar_Http_Request</a> class represents a standalone HTTP request (i.e., it\u2019s not an HTTP client, just a request).  It uses adapters, so you can (in the future) change between a <a href=\"http://php.net/curl\">cURL</a>, <a href=\"http://php.net/http\">pecl_http</a>, or <a href=\"http://php.net/sockets\">sockets</a> adapter \u2013 but for now, the only adapter is the <a href=\"http://php.net/stream\">streams-based</a> one.</p>\n<p>It\u2019s a <a href=\"http://paul-m-jones.com/blog/?p=188\">fluent class</a>, which makes the code a little prettier at times.  Here\u2019s a basic GET request that sends \u201c?foo=bar\u201d as the query string.</p>\n<pre>$request = Solar::factory('Solar_Http_Request');\n$request-&gt;setUri('http://example.com/index.php')\n        -&gt;setContent(array('foo' =&gt; 'bar'));\n\n$response = $request-&gt;fetch();\n</pre>\n<p>(You could also set the query string in the URI if you wanted, but using setContent() does all the esaping for you.)</p>\n<p>Here\u2019s the same thing as a POST:</p>\n<pre>$request = Solar::factory('Solar_Http_Request');\n\n$request-&gt;setUri('http://example.com/index.php')\n        -&gt;setContent(array('foo' =&gt; 'bar'))\n        -&gt;setMethod('post');\n\n$response = $request-&gt;fetch();\n</pre>\n<p>And here\u2019s a request with a lot of customization:</p>\n<pre>$request = Solar::factory('Solar_Http_Request');\n\n$request-&gt;setUri('http://example.com/index.php')\n        -&gt;setMethod('post')\n        -&gt;setHeader('X-Zim', 'Kneel before Zim!')\n        -&gt;setUserAgent('Irken Robot Gir')\n        -&gt;setBasicAuth('username', 'password')\n        -&gt;setContent(array('foo' =&gt; 'bar'));\n\n$response = $request-&gt;fetch();\n</pre>\n<p>Right now, Solar_Http_Request doesn\u2019t handle file uploads, but that functionality is planned for a future release.  However, it does provide SSL support through PHP streams.</p>\n<h3>Solar_Http_Response</h3>\n<p>What you get back from the request is a <a href=\"http://solarphp.com/class/Solar_Http_Response\">Solar_Http_Response</a> object.  You can explore the response by getting the response code, response text, HTTP version, headers, cookies, and content \u2013 all pretty standard stuff.</p>\n<p>You can also build a new Solar_Http_Response on your own and send that from your application.  This has the benefit of giving you relatively good control over headers, response codes, etc., so that you can pass the values around to different parts of your application before sending the final response.  (It also sanitizes header values automatically, which can be a nice addition for secure coding practices.)</p>\n<pre>$response = Solar::factory('Solar_Http_Response');\n$response-&gt;setResponseCode('200') // auto-sets response text to \"Ok\"\n         -&gt;setHeader('X-Example', 'Custom header value')\n         -&gt;setCookie('foo', 'bar') // sets cookie foo=bar\n         -&gt;setContent('&lt;p&gt;Hello world!&lt;/p&gt;');\n\n$response-&gt;display(); // sends sanitized headers and prints the content\n</pre>\n<p><a href=\"http://solarphp.com/class/Solar_Controller_Page\">Solar_Controller_Page</a>, for example, composes a Solar_Http_Response internally, which it then returns for sending.</p>\n"
}
