Back to the index of articles and examples

Sending raw JSON over POST

If everything goes fine, a transaction should have been initiated against this very same PHP script and one of the elements of a JSON array should be returned and shown in an alert.

The alert should read "there should be children here".

You might get an alert telling you that function json_decode does not exist. Run a phpinfo() on your server, your PHP interpreter might not have the JSON option compiled in.

My ISP does not have JSON compiled in its interpreter. The running code in my server actually uses Services_JSON, an external package coded in PHP so that it can run in any version of PHP.

Another gotcha is that you might not have the corresponding wrappers enabled so that you cannot open php://input. The PHP Manual lists several ways to read the raw POST input.


The code

<?php
	if ($_GET['data']) {
		$handle = fopen('php://input','r');
		$jsonInput = fgets($handle);
		$decoded = json_decode($jsonInput,true);
		echo $decoded[2]['title'];
		/* If you don't have native JSON support in PHP,
		   comment the two lines above and uncomment the lines below.
		   Adjust the path to the library as needed.
		require '/scripts/Services_JSON.php';
		$json = new Services_JSON();
		$decoded = $json->decode($jsonInput);
		echo $decoded[2]->title;
		*/
		fclose($handle);
	} else {
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>JSON over post</title> 
		<script type="text/javascript" src="http://yui.yahooapis.com/2.5.2/build/yahoo-dom-event/yahoo-dom-event.js" ></script>
		<script type="text/javascript" src="http://yui.yahooapis.com/2.5.2/build/connection/connection.js" ></script>
		<script type="text/javascript" src="http://yui.yahooapis.com/2.5.2/build/json/json.js" ></script>
	</head>
	<body>
	<dl>
		<dt>Self-referencing</dt>  
		<dd>see self-referencing.</dd>
	</dl>
	<pre>
	
	</pre>
	</body>
	<script>
		YAHOO.util.Connect.setDefaultPostHeader(false);
		YAHOO.util.Connect.initHeader("Content-Type","application/json; charset=utf-8");
		YAHOO.util.Connect.asyncRequest(
			'POST',
			'JSONsend.php?data=true',
			{
				success:function(o) {
					alert(o.responseText);
				},
				failure: function (o) {
					alert(o.statusText);
				}
			},
			YAHOO.lang.JSON.stringify([
				'Label 0',
				{type:'Text',label:'text label 1',title:'this is the tooltip for text label 1', editable:true},
				{type:'Text',label:'branch 1',title:'there should be children here', expanded:true, children:[
					'Label 1-0'
				]},
				{type:'Text',label:'text label 2',title:'this should be an href', href:'http://www.yahoo.com', target:'something'},
				{type:'HTML',html:'<a href="developer.yahoo.com/yui">YUI</a>',hasIcon:false},
				{type:'MenuNode',label:'branch 3',title:'this is a menu node', expanded:false, children:[
					'Label 3-0',
					'Label 3-1'
				]}

			])
		);
	</script>
</html>
<?php } ?>