Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

jsonp support? #17

Closed
Nevon opened this Issue · 5 comments

2 participants

@Nevon

This would be quite useful to get around the browser's cross-domain restrictions when making AJAX requests. I can't seem to find any documentation, other than the small bundled examples.

@Luracast Luracast was assigned
@Luracast

It is very easy to create any format that we find missing in Restler package. We need to create a format class that implements iFormat interface.

Jsonp is the easiest, we just need to wrap the JSON output inside a function call.

Here is the Jsonp formatter I quickly created :)

<?php
//jsonpformat.php
class JsonpFormat implements iFormat {

    const MIME = 'text/javascript';
    const EXTENSION = 'js';
    /*
     * JsonFormat is used internally
     * @var JsonFormat;
     */
    public $jsonFormat;
    public static $functionName = 'parseResponse';

    public function __construct() {
        $this->jsonFormat = new JsonFormat ();
        if (isset ( $_GET ['callback'] )) {
            self::$functionName = $_GET ['callback'];
        }
    }
    public function getMIMEMap() {
        return array (self::EXTENSION => self::MIME );
    }
    public function getMIME() {
        return self::MIME;
    }
    public function getExtension() {
        return self::EXTENSION;
    }
    public function encode($data, $human_readable = FALSE) {
        return self::$functionName . '(' . $this->jsonFormat->encode ( $data, $human_readable ) . ');';
    }
    public function decode($data) {
        return $this->jsonFormat->decode ( $data );
    }
    public function setMIME($mime) {
        //do nothing
    }
    public function setExtension($extension) {
        //do nothing
    }
} 

Save it in the same folder as restler.php and modify your index.php(gateway) to include JsonpFormat.

Something like

<?php
require_once '../../restler/restler.php';

#set autoloader
#do not use spl_autoload_register with out parameter
#it will disable the autoloading of formats
spl_autoload_register('spl_autoload');

$r = new Restler();
$r->setSupportedFormats('JsonpFormat','JsonFormat', 'XmlFormat');
$r->addAPIClass('BMI');
$r->handle();

Assuming that we are doing this for the Multi-format Example (Just for reference, not implemented in that URL yet). If we call

http://help.luracast.com/restler/examples/_003_multiformat/index.php/bmi.js

we will get

parseResponse({
  "bmi": 31.77,
  "message": "Obesity",
  "metric": {
    "height": "162.6 centimeter",
    "weight": "84 kilograms"
  },
  "imperial": {
    "height": "5 feet 4 inches",
    "weight": "185.19 pounds"
  }
});

we can also add a query string called callback to specify the function name

http://help.luracast.com/restler/examples/_003_multiformat/index.php/bmi.js?callback=myFunc

Now we will get

myFunc({
  "bmi": 31.77,
  "message": "Obesity",
  "metric": {
    "height": "162.6 centimeter",
    "weight": "84 kilograms"
  },
  "imperial": {
    "height": "5 feet 4 inches",
    "weight": "185.19 pounds"
  }
});

HTH,
Arul

@Luracast Luracast closed this
@Nevon

That's a very informative answer. Thank you. I had no major problems implementing it.

@Luracast

For the benefit of Restler community, please share even the minor problems you faced and how you solved it. Looking forward for your contributions :)

@Nevon

Well, there's really not that much to tell. The only real difference is that I wanted to make it more transparent. So rather than have the consumer add the .js suffix, I made it so that it returns ordinary json if the callback parameter is missing, and jsonp if it exists. There's probably a prettier way to do it, but I just made jsonp the standard format and added a selection to the encode method.

@Luracast

got it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.