<?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>Andrew Vayanis &#187; REST</title>
	<atom:link href="http://www.vayanis.com/tag/rest/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.vayanis.com</link>
	<description>Developer, Gamer, Thinker</description>
	<lastBuildDate>Wed, 19 May 2010 15:58:46 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Sending POST requests with PHP</title>
		<link>http://www.vayanis.com/2007/08/28/sending-post-requests-with-php/</link>
		<comments>http://www.vayanis.com/2007/08/28/sending-post-requests-with-php/#comments</comments>
		<pubDate>Tue, 28 Aug 2007 16:13:06 +0000</pubDate>
		<dc:creator>Andrew Vayanis</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[REST]]></category>

		<guid isPermaLink="false">http://www.vayanis.com/2007/08/28/sending-post-requests-with-php/</guid>
		<description><![CDATA[I recently found myself writing a REST web service, which, in my opinion, is the easiest way to create a web service, for a project at work.  Using XML.com&#8217;s How to Create a REST protocol as my Guide, I went to work.  However, when I tried to create a service to update some [...]]]></description>
			<content:encoded><![CDATA[<p>I recently found myself writing a REST web service, which, in my opinion, is the easiest way to create a web service, for a project at work.  Using XML.com&#8217;s <a href="http://www.xml.com/pub/a/2004/12/01/restful-web.html">How to Create a REST protocol</a> as my Guide, I went to work.  However, when I tried to create a service to update some data, I wasn&#8217;t sure how to send my data in a POST request using PHP.  After some stumbling around, I found what I was looking for on <a href="http://netevil.org/blog/2006/nov/http-post-from-php-without-curl">Wez Furlong&#8217;s blog</a> which led me to PHP&#8217;s documentation on <a href="http://www.php.net/manual/en/wrappers.http.php">HTTP and HTTPS wrappers</a>.</p>
<p>The following is a code snippet from the HTTP and HTTPS documentation that shows how to easily send data using HTTP POST:<br />
<span id="more-24"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$postdata</span> <span style="color: #339933;">=</span> <span style="color: #990000;">http_build_query</span><span style="color: #009900;">&#40;</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'var1'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'some content'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'var2'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'doh'</span>
    <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$opts</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http'</span> <span style="color: #339933;">=&gt;</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'method'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'POST'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'header'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Content-type: application/x-www-form-urlencoded'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'content'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$postdata</span>
    <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$context</span>  <span style="color: #339933;">=</span> <span style="color: #990000;">stream_context_create</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$opts</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://example.com/submit.php'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #000088;">$context</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Quick Explanation</h3>
<ol>
<li><a href="http://us.php.net/http_build_query">http_build_query</a>: Generates an URL-encoded string from an associative array.  Example: array(&#8216;key1&#8242; => &#8216;value1&#8242;, &#8216;key2&#8242; => &#8216;value2&#8242;) becomes &#8220;key1=value1&#038;key2=value2&#8243;
<p>This properly encodes the data for the post transaction.</p>
</li>
<li><a href="http://us.php.net/manual/en/function.stream-context-create.php">stream_context_create</a>: Creates and returns a resource to a stream context from an associative array of arrays in the format <em>$arr['wrapper']['option'] = $value</em>
<p>This creates a linear output stream to be sent during the request transmission.</p>
</li>
<li><a href="http://us3.php.net/manual/en/function.file-get-contents.php">file_get_contents</a>: Acts exactly like file(); except that it reads an entire file into a string.
<p>This requests a file resource and puts the result into a string.</p>
</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.vayanis.com/2007/08/28/sending-post-requests-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
