CodeIgniter Rest Api - php

I'm implementing the api in codeigniter using the restcontroller and format files shown in the gist below, once inserted I implement the Api.php file in the path specified below but when I try to call the api as a test I get the following error 404 not found, what is it and how can I fix this?
url to call api get: http://localhost/api/Api/testget
I have this file (rest controller and format)
https://gist.github.com/riccardopirani/8878fcda16cae2ad45e4b1c9e624f619
Php Code(in folder /controllers/api/Api.php:
<?php
use Restserver\Libraries\REST_Controller;
require(APPPATH.'libraries/REST_Controller.php');
class Books extends REST_Controller {
function testget()
{
$data = array("message"=>"RESTfull API sample");
$this->response($data);
}
}

Write method name as test_get. Keep class and file name same, then call (get) http://localhost/[projectname]/index.php/api/book/test

Related

How can I send an API Request directly from Controller in Laravel 5.2?

I am currently working on a project that implement API integration on several sites.
Now as I go along, I noticed that I was using too much AJAX Request using JQuery on my project (of which codes viewable on page source) and I wanted to limit that as much as possible.
Is there a way to send an API Request directly from any controller in Laravel 5.2 just like the code below?
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HttpRequestController extends Controller
{
public function PostRequest($uri, $data) {
//functional code that will post the given data to the specified uri
}
public function GetRequest($uri) {
//functional code that will retrieve the given data via the specified uri
}
}
You can use Guzzle https://github.com/guzzle/guzzle library to make HTTP requests.

Codeigniter RESTful API Server - XML Error?

I've read the following topics/tutorials:
Codeigniter RESTful API Server
http://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter--net-8814
https://github.com/chriskacerguis/codeigniter-restserver
And I still can't figure out why I'm with route problems and XML problems.
My webservice controller is in the folder controllers/api/webservice.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH.'/libraries/RESTful/REST_Controller.php';
class webservice extends REST_Controller{
function get() {
$data = array();
$data['name'] = "TESTNAME";
$this->response($data);
}
}
In the tutorials there is no need to add routes, and in order to receive the XML page error I needed to add the following route or it wouldn't work:
$route['api/webservice/get'] = 'api/webservice/get';
My structure folder of codeIgniter:
> application
> config
rest.php (did not change anything from the GitHub download)
> controllers
> api
webservice.php
key.php
> libraries
> RESTful
REST_Controller.php (changed line 206 to: $this->load->library('RESTful/format');)
format.php
From the tutorial, the following link works without routes:
http://localhost/testRestful/index.php/api/example/users
Mine only works with route
http://localhost/myproject/index.php/api/webservice/get
And I receive the following error:
It does not say anything else. I can't figure out which file is the error talking about.
you cannot write get function if you use REST_Controller.
give that function name test_get.
class webservice extends REST_Controller{
function test_get() {
$data = array();
$data['name'] = "TESTNAME";
$this->response($data);
}
}
Now you can access the page with this link
http://localhost/myproject/index.php/api/webservice/test
_get and _post is added end of the function to detect either it is get request or post requerst.

CakePHP admin routing for REST API

frontend of my CakePHP app is based on the REST API, each controller has user and admin methods.
I would like to run admin method on controller with classic Cake view.
But if i type into browser URL like this:
http://myproject.loc/admin/cards/add/
I get only:
{
code: 500,
name: "View file api/app/View/Cards/json/admin_add.ctp" is missing.",
message: "View file api/app/View/Cards/json/admin_add.ctp" is missing.",
url: "/admin/cards/add/"
}
Question is:
How can i set right URL for admin routes? (without json prefix)
Read Json and XML Views in CakePHP and read it properly. A common mistake is that people forget to add the RequestHandler component to their components array. The component will take care of getting the right layout set for you and the data serialized.
If you really want to insist on removing the .json suffix from the URL but calling the same URL via browser and AJAX you'll have to make sure that you request the right content type. Check the headers you send.
I'm not sure if the RequestHandler will send you the right response if you only request json (application/json) in the header without using the suffix in the URL, it should work, give it a try.
Here is the ref.
http://book.cakephp.org/2.0/en/views/json-and-xml-views.html
You need to add following line into the your controller function
I hope your encoding it to the json in the following way
$arrayData = array(...);//as example
class PostsController extends AppController {
public function index() {
$this->autoRender = false;
$this->layout = null;
$this->set(compact('posts', 'comments'));
}
}
// View code - app/View/Posts/json/index.ctp
foreach ($posts as &$post) {
unset($post['Post']['generated_html']);
}
echo json_encode(compact('posts', 'comments'));

CakePHP - creating an API for my cakephp app

I have created a fully functional CakePHP web application. Now, i wanna get it to the next level and make my app more "opened". Thus i want to create an RESTful API. In the CakePHP docs,i found this link (http://book.cakephp.org/2.0/en/development/rest.html) which describes the way to make your app RESTful. I added in the routes.php the the two lines needed,as noted at the top of the link,and now i want to test it.
I have an UsersController.php controller,where there is a function add(),which adds new users to databases. But when i try to browse under mydomain.com/users.json (even with HTTP POST),this returns me the webpage,and not a json formatted page,as i was expecting .
Now i think that i have done something wrong or i have not understood something correctly. What's actually happening,can you help me a bit around here?
Thank you in advace!
You need to parse JSON extensions. So in your routes.php file add:
Router::parseExtensions('json');
So a URL like http://example.com/news/index you can now access like http://example.com/news/index.json.
To actually return JSON though, you need to amend your controller methods. You need to set a _serialize key in your action, so your news controller could look like this:
<?php
class NewsController extends AppController {
public function index() {
$news = $this->paginate('News');
$this->set('news', $news);
$this->set('_serialize', array('news'));
}
}
That’s the basics. For POST requests, you’ll want to use the RequestHandler component. Add it to your controller:
<?php
class NewsController extends AppController {
public $components = array(
'RequestHandler',
...
);
And then you can check if an incoming request used the .json file extension:
public function add() {
if ($this->RequestHandler->extension == 'json') {
// JSON request
} else {
// do things as normal
}
}

REST Service not working-Code Igniter

I want to implement a Phill Sturgeon CodeIgniter RESTServer library in my project. I copied the files rest.php, Format.php, REST_Controler.php in folders config,library,library respectively.
I created my controller called services with following code:
<?php
require(APPPATH.'/libraries/REST_Controller.php');
class services extends REST_Controller {
function Teams_get(){
$teamNames=$this->team_model->getTeamNames();
$this->response($teamNames);
}
TeamModel is autoloaded in my autoload.php. When I want to run Teams_get method in my browser result is:
{"status":false,"error":"Unknown method."}
I read here that I should change REST_Controler.php configuration file, but this change should only be done if POST methods are not working.
My services should be public, so I don't need authentication methods.
What's wrong here?
When calling your API, the URL should just be the name of the method, without the _get (or _post). That is added by the REST server depending on how the URL is called (GET vs POST).
So, to call your Teams_get method, you want to send a GET request to the URL /services/Teams (not /services/Teams_get).
Docs: https://github.com/philsturgeon/codeigniter-restserver#handling-requests

Categories