<?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>Geekality &#187; JSONP</title>
	<atom:link href="http://www.geekality.net/tag/jsonp/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.geekality.net</link>
	<description>With a hint of Social Ineptitude</description>
	<lastBuildDate>Sun, 05 Feb 2012 17:53:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>PHP: How to easily provide JSON and JSONP</title>
		<link>http://www.geekality.net/2010/06/27/php-how-to-easily-provide-json-and-jsonp/</link>
		<comments>http://www.geekality.net/2010/06/27/php-how-to-easily-provide-json-and-jsonp/#comments</comments>
		<pubDate>Sun, 27 Jun 2010 18:03:28 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[CORS]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[JSONP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Snippet]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=1021</guid>
		<description><![CDATA[Would you like to grab some server-side data through an AJAX call? For example by using the handy jQuery.ajax method? A good data format to use then is JavaScript Object Notation, more commonly known as JSON. Providing data in the &#8230; <a href="http://www.geekality.net/2010/06/27/php-how-to-easily-provide-json-and-jsonp/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Would you like to grab some server-side data through an AJAX call? For example by using the handy <a href="http://api.jquery.com/jQuery.ajax/">jQuery.ajax</a> method?</p>
<p>A good data format to use then is <a href="http://en.wikipedia.org/wiki/JSON">JavaScript Object Notation</a>, more commonly known as JSON. Providing data in the JSON format with PHP is super duper simple  <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt='8)' class='wp-smiley smiley-3' /> </p>
<p><span id="more-1021"></span></p>
<h2>JSON</h2>
<p>All you need to do on the server side is to set the <code class="codecolorer text default"><span class="text">content-type</span></code> to <code class="codecolorer text default"><span class="text">application/json</span></code>, encode your data using the <a href="http://php.net/json_encode">json_encode</a> function and output it.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content-type: application/json; charset=utf-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>You can test it out for example in the <a href="http://getfirebug.com/">FireBug</a> console by running this line:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>url<span style="color: #339933;">:</span> <span style="color: #3366CC;">'data.php'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #006600; font-style: italic;">// Response: [1,2,3,4,5,6,7,8,9]</span></div></div>
<p>You should see the request being done in the console and also in the Net tab. If it didn&#8217;t work, you might be subject to the following:</p>
<blockquote><p>Due to browser security restrictions, most &#8220;Ajax&#8221; requests are subject to the <a title="Same Origin Policy on Wikipedia" href="http://en.wikipedia.org/wiki/Same_origin_policy">same origin policy</a>; the request can not successfully retrieve data from a different domain, subdomain, or protocol.</p></blockquote>
<p>A nice and simple solution to that problem is JSON with padding, also known as JSONP.</p>
<h2>JSONP</h2>
<p>As stated on <a href="http://en.wikipedia.org/wiki/JSON#JSONP">Wikipedia</a>,</p>
<blockquote><p>JSONP or &#8220;JSON with padding&#8221; is a complement to the base JSON data format, a usage pattern that allows a page to request and more meaningfully use JSON from a server other than the primary server.</p></blockquote>
<p>I&#8217;m not sure I get everything about JSONP, but I have used it and I know that  it works <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' class='wp-smiley smiley-13' /> It&#8217;s almost just as easy as plain JSON actually, and all that&#8217;s needed is to wrap the JSON encoded data in a callback function provided as a GET parameter.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content-type: application/json; charset=utf-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'callback'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'('</span><span style="color: #339933;">.</span><span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">')'</span><span style="color: #339933;">;</span></div></div>
<p>You can test it by running</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>url<span style="color: #339933;">:</span> <span style="color: #3366CC;">'data.php'</span><span style="color: #339933;">,</span> dataType<span style="color: #339933;">:</span><span style="color: #3366CC;">'jsonp'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #006600; font-style: italic;">// Response: jsonp1277656587731([1,2,3,4,5,6,7,8,9])</span></div></div>
<p>This time you won&#8217;t see the call in the FireBug console though, but only in the Net tab. That&#8217;s because the data is loaded in a script tag instead of through an actual AJAX call. Thankfully jQuery handles all of that for you behind the scenes, so you don&#8217;t have to worry about it at all. You&#8217;ll get the same JSON data in your response handler, ready to be used however you want to <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' class='wp-smiley smiley-1' /> </p>
<p>What if you&#8217;d like to provide both formats?</p>
<h2>JSON and JSONP together</h2>
<p>Easy as cake actually. You just need to check if the callback parameter is set or not:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content-type: application/json; charset=utf-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$json</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'callback'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; ? <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$_GET['callback']}</span>(<span style="color: #006699; font-weight: bold;">$json</span>)&quot;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">:</span> <span style="color: #000088;">$json</span><span style="color: #339933;">;</span></div></div>
<p>And that&#8217;s all! Although there are some additions you could do if you want:</p>
<h2>Cross-Origin Resource Sharing (CORS)</h2>
<p>When testing this you might have noticed it will not work cross-domain. This is because of security stuff in your browser. So, if you want everyone to be able to get access to some JSON data of yours, you need to flag that this is OK. One way to do this, is through CORS:</p>
<blockquote><p>Cross-Origin Resource Sharing (CORS) is a specification that enables a truly open access across domain-boundaries.<br />
&#8230;<br />
CORS defines how browsers and servers communicate when accessing sources across origins using HTTP headers to allow both the browser and the server to know enough about each other to determine if the request or response should succeed or fail. &#8212; <a href="http://enable-cors.org">enable-cors.org</a></p></blockquote>
<p>You can read more about it on the <a href="http://enable-cors.org">enable-cors.org</a> website, but all you should need to do if you want your JSON data accessible from everywhere is to add a single header.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content-type: application/json; charset=utf-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;access-control-allow-origin: *&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Rest of script...</span></div></div>
<p>It should now be accessible from everywhere <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' class='wp-smiley smiley-1' /> </p>
<h2>Added security</h2>
<p>Since the sender can send whatever they want as a callback, you could potentially have someone injecting malicious code there. I&#8217;m not sure how likely this is to happen, but it is a possibility. Could for example be used to steal cookies. Not the baked kind&#8230; So, although it does make things a bit uglier, it&#8217;s better to be safe than sorry I suppose? So I did <a href="/?p=1739">some research</a> and ended up with a function I could use to check that the callback is indeed a valid JavaScript identifier. Here&#8217;s the code with the added security.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content-type: application/json; charset=utf-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">function</span> is_valid_callback<span style="color: #009900;">&#40;</span><span style="color: #000088;">$subject</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$identifier_syntax</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/^[$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\x{200C}\x{200D}]*+$/u'</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000088;">$reserved_words</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'break'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'do'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'instanceof'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'typeof'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'case'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'else'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'new'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'var'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'catch'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'finally'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'return'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'void'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'continue'</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'for'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'switch'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'while'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'debugger'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'function'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'this'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'with'</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'default'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'if'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'throw'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'delete'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'in'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'try'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'class'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'enum'</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'extends'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'super'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'const'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'export'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'import'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'implements'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'let'</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'private'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'public'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'yield'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'interface'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'package'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'protected'</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'static'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'null'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'true'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'false'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$identifier_syntax</span><span style="color: #339933;">,</span> <span style="color: #000088;">$subject</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span> <span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mb_strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$subject</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UTF-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$reserved_words</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$json</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># JSON if no callback<br />
</span><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'callback'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$json</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># JSONP if valid callback<br />
</span><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>is_valid_callback<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'callback'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$_GET['callback']}</span>(<span style="color: #006699; font-weight: bold;">$json</span>)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Otherwise, bad request<br />
</span><span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'status: 400 Bad Request'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">400</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Please comment if you have any feedback <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' class='wp-smiley smiley-1' /> </p>
<p><ins datetime="2010-12-11T06:28:34+00:00"><strong>Update:</strong> Added charset to the content-type header as recommended in <a href="http://stackoverflow.com/q/3128062/39321#3128948">an answer</a> to a question I posted about this on StackOverflow.</ins></p>
<p><ins datetime="2011-08-03T15:14:15+00:00"><strong>Update:</strong> Added stuff about callback checking. Recommended in new answers to <a href="http://stackoverflow.com/q/3128062/39321">my question at StackOverflow</a>.</ins></p>
<p><ins datetime="2011-10-31T18:57:00+00:00"><strong>Update:</strong> Discovered an easy way to compress the JSON data! <a href="/?p=1847">Blogged about it</a> too <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' class='wp-smiley smiley-1' /> </ins></p>
<p><ins datetime="2012-01-08T13:23:18+00:00"><strong>Update:</strong> Added some info about <a href="http://enable-cors.org">cross-origin resource sharing</a></ins></p>
<p class="wp-flattr-button"></p> <p><a href="http://www.geekality.net/?flattrss_redirect&amp;id=1021&amp;md5=c00077187379876f01ba7e253aab6dde" title="Flattr" target="_blank"><img src="http://www.geekality.net/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2010/06/27/php-how-to-easily-provide-json-and-jsonp/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
	</channel>
</rss>

