{"id":29,"date":"2009-05-08T22:30:15","date_gmt":"2009-05-09T02:30:15","guid":{"rendered":"http:\/\/www.carolinamantis.com\/wordpress\/?p=29"},"modified":"2009-05-08T22:35:06","modified_gmt":"2009-05-09T02:35:06","slug":"creating-a-jsonp-data-provider-in-php","status":"publish","type":"post","link":"http:\/\/www.carolinamantis.com\/wordpress\/?p=29","title":{"rendered":"Creating a JSONP Data Provider in PHP"},"content":{"rendered":"<div class=\"wp_plus_one_button\" style=\"margin: 0 8px 8px 0; float:left; \"><g:plusone href=\"http:\/\/www.carolinamantis.com\/wordpress\/?p=29\" callback=\"wp_plus_one_handler\"><\/g:plusone><\/div><p>PHP 5.2 or PECL json 1.2 has the ability to convert data structures into JSON:<\/p>\n<pre name=\"code\" class=\"php\">\r\n$data = array('zero', 'one', 'two', 'three');\r\n$json = json_encode($data);\r\necho $json\r\n<\/pre>\n<p>However, if you are deploying to a lowered PHP version, either because your hosting provider hasn&#8217;t caught up or you&#8217;re using Red Hat Enterprise (even the latest 5.3 only has PHP 5.1.2), then you need an alternate solution.  I&#8217;ve visited <a href=\"http:\/\/www.json.org\">http:\/\/www.json.org<\/a> and found a good package: <a href=\"http:\/\/pear.php.net\/package\/Services_JSON\">PEAR::Services_JSON<\/a> (aka pear install Services_JSON).  Although it&#8217;s in the beta channel, I&#8217;ve had good luck with it.<\/p>\n<p>The equivalent code to the above is:<\/p>\n<pre name=\"code\" class=\"php\">\r\nrequire('Services\/JSON.php');\r\n$data = array('zero', 'one', 'two', 'three');\r\n$encoder = new Services_JSON(); \r\n$json = $encoder->encode($data);\r\necho $json;\r\n<\/pre>\n<p>So, that is how you get a JSON string.  What if you want to actually emit the JSON in an AJAX-ically useful way?  Just add the PHP header function: <\/p>\n<pre name=\"code\" class=\"php\">\r\nrequire('Services\/JSON.php');\r\n$data = array('zero', 'one', 'two', 'three');\r\n$encoder = new Services_JSON(); \r\n$json = $encoder->encode($data);\r\nheader(\"Content-type: application\/json\");\r\necho $json;\r\n<\/pre>\n<p>Now, for the final touch.  Turning out standard JSON into JSONP.  The only difference is that we need a JavaScript function definition wrapping the code:<\/p>\n<pre name=\"code\" class=\"php\">\r\nrequire('Services\/JSON.php');\r\n$data = array('zero', 'one', 'two', 'three');\r\n$encoder = new Services_JSON(); \r\n$json = $encoder->encode($data);\r\nheader(\"Content-type: application\/json\");\r\necho 'callbackname (' . $json . ');';\r\n<\/pre>\n<p>We&#8217;ve added a function wrapper around the JSON.  That allows for the data to be retrieved using the jQuery API.  However, we can&#8217;t hard code the callbackname as you see above.  We need to use the passed in value from jQuery.<\/p>\n<pre name=\"code\" class=\"js\">\r\n$(function() {\r\n     $.getJSON('provider.php?callback=?',\r\n                    {},  \/\/ No additional parameters sent\r\n                    function (data) {\r\n                         \/\/ data is now JSON object instantiated from retrieved info\r\n                         alert(data[0]) \/\/ Will show 'zero' in the alert that pops up\r\n                    })\r\n     });\r\n});\r\n<\/pre>\n<p>That strange second question mark in the URL will be replaced by a jQuery created random number string that is expected to be returned in your callback name.<\/p>\n<p>Save the code below as provider.php:<\/p>\n<pre name=\"code\" class=\"php\">\r\nrequire('Services\/JSON.php');\r\n$data = array('zero', 'one', 'two', 'three');\r\n$encoder = new Services_JSON(); \r\n$json = $encoder->encode($data);\r\nheader(\"Content-type: application\/json\");\r\necho $_GET['callback'] . ' (' . $json . ');';\r\n<\/pre>\n<p>If you are using the outstanding <a href=\"http:\/\/www.getfirebug.com\">Firebug<\/a> add-on for Firefox, you&#8217;ll be able to see the GET request made by the page containing the jQuery code as well as the JSONP response from the script.<\/p>\n<p>Why go through this trouble?  Because we can now put provider.php on any server, regardless of whether the page calling provider.php is in the same domain or not.  JSONP gets around the normal AJAX &#8220;same domain&#8221; restriction.<\/p>\n<p>Now that we have our own provider, we can create simple arrays, complex arrays, and even objects that can be turned into JSON and sent to jQuery enabled pages, regardless of domain.<\/p>\n<p>For additional information, see this <a href=\"http:\/\/www.ibm.com\/developerworks\/library\/wa-aj-jsonp1\/\">IBM Developerworks article<\/a>.  It lists some of the existing JSONP services, including the URL to use with the jQUERY ajax invocations.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"wp_plus_one_button\" style=\"margin: 0 8px 8px 0; float:left; \"><g:plusone href=\"http:\/\/www.carolinamantis.com\/wordpress\/?p=29\" callback=\"wp_plus_one_handler\"><\/g:plusone><\/div><p>PHP 5.2 or PECL json 1.2 has the ability to convert data structures into JSON: $data = array(&#8216;zero&#8217;, &#8216;one&#8217;, &#8216;two&#8217;, &#8216;three&#8217;); $json = json_encode($data); echo $json However, if you are deploying to a lowered PHP version, either because your hosting provider hasn&#8217;t caught up or you&#8217;re using Red Hat Enterprise (even the latest 5.3 only [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,7,8],"tags":[],"_links":{"self":[{"href":"http:\/\/www.carolinamantis.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/29"}],"collection":[{"href":"http:\/\/www.carolinamantis.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.carolinamantis.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.carolinamantis.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.carolinamantis.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=29"}],"version-history":[{"count":23,"href":"http:\/\/www.carolinamantis.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/29\/revisions"}],"predecessor-version":[{"id":52,"href":"http:\/\/www.carolinamantis.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/29\/revisions\/52"}],"wp:attachment":[{"href":"http:\/\/www.carolinamantis.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.carolinamantis.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.carolinamantis.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}