dot
dot

Reputation: 15660

Codeigniter / MVC way of calling a rest api

We have a web app (let's call it widget app) that contains data I need to integrate with a new Codeigniter app that I’m building.

I have a controller called objects and lets say it will have a method called getallobjects. This method actually has to return data from the widget application.

There is an “API” of sorts for the widget app, but the way that I call it in a RESTful way by getting a URL like:

   http://myserver/widget/abc.php?method=getsomething

This returns a bunch of json encoded data.

How would I use this type of an API in my MVC CI app?

So far, this is what my controller looks like:

 class Objects extends CI_Controller {
     public function __construct()
     {
         parent::__construct();
         $this->load->helper('url');
     }

     public function getallobjects()
     {
         $data['objectlist'] = ????/* This is where I need to call the rest api and get json data. */

         $data['main_content']='objects';
         $this->load->view('includes/template', $data);
     }
  }  

Upvotes: 1

Views: 13419

Answers (2)

Chandrika Prasad Shah
Chandrika Prasad Shah

Reputation: 773

$your_url = "http://myserver/widget/abc.php?method=getsomething"; //put your url here
$data['objectlist'] = file_get_contents($your_url);

Upvotes: 0

dm03514
dm03514

Reputation: 55962

you can call file_get_contents($your_url)

http://php.net/manual/en/function.file-get-contents.php

to fetch the response.

Additionally you can use php curl wrapper for finer control over your request. http://php.net/manual/en/book.curl.php

Upvotes: 3

Related Questions