1

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);
     }
  }  

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

Although this may answer the question, some comments and/or explanation would be nice. That will help other users as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.