Search code examples
restcross-domainrestler

restler 3 cross domain not working


My restler 3 api works fine on local test server and works fine on production server if calls from that same server, but if I make the call remotely then it fails.

Using the same rest client with the luracast online examples it works fine with remote call so must be something in my configuration (either my api or my production server).

I found mention of need to send headers and so tried adding these headers to index.php file:

header('Access-Control-Allow-Origin:  *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, PATCH, DELETE');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: *');

But that didn't help. Using RESTClient addon in firefox, I can see that those headers are sent, and the browser will show the data both locally and remotely, whether I use those header commands or not.

Here's a sample call: https://api.masterpiecesolutions.org/v1/artists/?key=A4oxMOYEUSF9lwyeFuleug==

My index.php for that call uses this, with 2nd param to map to root level

$r->addAPIClass('Artists', '');

Don't know if that is relevant.

Also, the production server is Amazon EC2, so perhaps has something to do with security policy?

Or, maybe it's some other header issue? In google chrome, using Advanced Rest Client extension, it gives status of 403 Forbidden and Content-Type is text/plain (whether using local or remote server) so it won't work at all, unlike the firefox addon.

I also see use of $_SERVER['HTTP_ORIGIN'] in Restler.php, and this doesn't appear to be supported everywhere yet?


Solution

  • The problem, for me at least, was using SSL and the restclient class didn't accommodate that.

    So I added (to my RestClient.class.php from phpclasses.org) curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false); // for SSL and now it works.

    Also required was setting public static $crossOriginResourceSharing = true; in Defaults.php for Restler 3.