enabling cors in codeigniter ( restserver by @chriskacerguis )
Try adding OPTIONS
to the allowed methods.
header("Access-Control-Allow-Methods: GET, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Content-Length, Accept-Encoding");
and return immediately when the request is method 'OPTIONS' once you have set the headers.
if ( "OPTIONS" === $_SERVER['REQUEST_METHOD'] ) {
die();
}
See also this answer.
Angular sends a W3C CORS spec compliant preflight request that will check for the right allowed methods before actually attempting it.
Personally, I find the Mozilla Developer Network CORS page a bit easier to read on the matter to help understand the flow of CORS.
HTTP OPTIONS error in Phil Sturgeon's Codeigniter Restserver and Backbone.js
I encountered exactly the same problem. To solve it I have a MY_REST_Controller.php in core and all my REST API controllers use it as a base class. I simply added a constructor like this to handle OPTIONS requests.
function __construct() {
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
$method = $_SERVER['REQUEST_METHOD'];
if($method == "OPTIONS") {
die();
}
parent::__construct();
}
This just checks if the request type is OPTIONS and if so just dies out which return a code 200 for the request.
Invalid Api key using RestServer
i faced the same issue .don't mention put/get/post in URL ,RestServer itself recognizes the request nature base on parameter you pass two step required to solve your problem .
1st Step :
http://localhost/projects/myapi/key/index_put.php
must change to :
http://localhost/projects/myapi/key/index.php
2nd step:
create an api kay using sha1
(max 40 character) in the keys table (table structure show in config/rest.php file),
enter 1 in is_private_key
field and ::1 in ip_address
field.
create the record ,and check it again .
Invalid Api key using RestServer
i faced the same issue .don't mention put/get/post in URL ,RestServer itself recognizes the request nature base on parameter you pass two step required to solve your problem .
1st Step :
http://localhost/projects/myapi/key/index_put.php
must change to :
http://localhost/projects/myapi/key/index.php
2nd step:
create an api kay using sha1
(max 40 character) in the keys table (table structure show in config/rest.php file),
enter 1 in is_private_key
field and ::1 in ip_address
field.
create the record ,and check it again .
Invalid API key as a reponse of PUT method using RESTServer codeigniter
I've seen some API's that do not look at the GET params if you make a POST or PUT request for credentials or are inconsistent in how they do it.
Really, credentials should go in headers either via the Authorize header or a custom one for many reasons like 'not logging credentials to access logs', but I digress.
In this case you can try:
- Put (no pun) the
X-API-KEY=XXX
inside the body of thePUT
just to see if this works - See if/how the library accepts the API key in a header
Looking at this library in particular (https://github.com/chriskacerguis/codeigniter-restserver), they do support the header X-API-KEY
. This should be where you put the key for ALL requests--it's best practice not to pass them as url params.
Here's the commandline example using curl from their Github project.
curl -X POST -H "X-API-KEY: some_key_here" http://example.com/books
In PHP you can use curl to set header like this:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-API-KEY: XXX'));
Related Topics
Sending Data Along with a Redirect in Codeigniter
Doctrine2 Association Mapping with Conditions
Post Data and Retrieve the Response Using PHP Curl
Apache2 on Ubuntu - PHP Files Downloading
How to Redirect to Different Url Based on Roles in Symfony 2
Request Exceeded the Limit of 10 Internal Redirects
Remove Everything from the First Occurrence of a Character to the End of a String in PHP
How to See the Actual Xml Generated by PHP Soap Client Class
Interview Question: in PHP, Is 123==0123
Bulk Rename Files in a Folder - PHP
How to Eliminate PHP5 Strict Standards Errors
Can PHP Asynchronously Use Sockets
Declaring a Global Variable Inside a Function