<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Query7 &#187; no CURL</title>
	<atom:link href="http://query7.com/tag/no-curl/feed" rel="self" type="application/rss+xml" />
	<link>http://query7.com</link>
	<description>PHP, Javascript, Python and Web Development</description>
	<lastBuildDate>Sat, 25 Jun 2011 21:29:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>HTTP Request without CURL</title>
		<link>http://query7.com/http-request-without-curl</link>
		<comments>http://query7.com/http-request-without-curl#comments</comments>
		<pubDate>Thu, 08 Jan 2009 09:46:37 +0000</pubDate>
		<dc:creator>Prakash Mohanty</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[HTTP Request]]></category>
		<category><![CDATA[no CURL]]></category>

		<guid isPermaLink="false">http://www.webdevelopmentbits.com/?p=340</guid>
		<description><![CDATA[<p>A short tutorial on how to make a simple object for HTTP request without the need of using CURL.<br />
The way to accomplish that is using the function <code>stream_context_create</code> to prepare the string, and then you use <code>fopen</code> and <code>stream_get_contents</code> to get the response.</p>
<p><code>&#60;?php</code></p>
<pre>
class Custom_Http_Request
{
    private $_url;
    private $_body;
    private $_method = 'POST';
    private $_headers = array();
    private $_response;
    private $_stream;

    public function __construct($url, $body)
    {
        if (empty($url) &#124;&#124; empty($body)) {
            throw new Exception('Both URL and BODY are required '
                              . 'for fetching the request.');
        }

        $this->_url  = $url;
        $this->_body = $body;
    }

    public function setMethod($method)
    {
        if ('POST' == $method &#124;&#124; 'GET' == $method) {
            $this->_method = $method;
            return $this;
        }

        throw new Exception('Invalid method set.');
    }

    public function addHeader(array $header)
    {
        if (!empty($header)) {
            $this->_headers[] = $header;
            return $this;
        }

        throw new Exception('The headers are empty.');
    }

    public function getMethod()
    {
        return $this->_method;
    }

    public function getBody()
    {
        return $this->_body;
    }

    public function getHeaders()
    {
        return $this->_headers;
    }

    public function getResponse()
    {
        if (is_null($this->_stream)) {
            $this->_openStream();
        }

        if (is_null($this->_response)) {
            $this->_response = @stream_get_contents($this->_stream);

            if (false === $this->_response) {
                throw new Exception('It is not possible to '
                                  . 'read from the response.');
            }
        }</pre><p>&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>A short tutorial on how to make a simple object for HTTP request without the need of using CURL.<br />
The way to accomplish that is using the function <code>stream_context_create</code> to prepare the string, and then you use <code>fopen</code> and <code>stream_get_contents</code> to get the response.</p>
<p><code>&lt;?php</code></p>
<pre>
class Custom_Http_Request
{
    private $_url;
    private $_body;
    private $_method = 'POST';
    private $_headers = array();
    private $_response;
    private $_stream;

    public function __construct($url, $body)
    {
        if (empty($url) || empty($body)) {
            throw new Exception('Both URL and BODY are required '
                              . 'for fetching the request.');
        }

        $this->_url  = $url;
        $this->_body = $body;
    }

    public function setMethod($method)
    {
        if ('POST' == $method || 'GET' == $method) {
            $this->_method = $method;
            return $this;
        }

        throw new Exception('Invalid method set.');
    }

    public function addHeader(array $header)
    {
        if (!empty($header)) {
            $this->_headers[] = $header;
            return $this;
        }

        throw new Exception('The headers are empty.');
    }

    public function getMethod()
    {
        return $this->_method;
    }

    public function getBody()
    {
        return $this->_body;
    }

    public function getHeaders()
    {
        return $this->_headers;
    }

    public function getResponse()
    {
        if (is_null($this->_stream)) {
            $this->_openStream();
        }

        if (is_null($this->_response)) {
            $this->_response = @stream_get_contents($this->_stream);

            if (false === $this->_response) {
                throw new Exception('It is not possible to '
                                  . 'read from the response.');
            }
        }

        return $this->_response;
    }

    private function _assemble()
    {
        $params = array(
        	'http' => array(
        		'method'  => $this->_method,
        		'content' => $this->_body
            )
        );

        if (!empty($this->_headers)) {
            $params['http']['header'] = $this->_headers;
        }

        return stream_context_create($params);
    }

    private function _openStream()
    {
        $this->_stream = <code>@fopen</code>($this->_url, 'rb', false, $this->_assemble());

        if (!$this->_stream) {
            throw new Exception('It was not possible to '
                              . "connect to {$this->_url}.");
        }
    }
}
</pre>
<p>More on this in my next post.</p>
]]></content:encoded>
			<wfw:commentRss>http://query7.com/http-request-without-curl/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

