简体   繁体   中英

calling Rest API in codeigniter controller function

I have set up Rest API's in codeigniter which are to be consumed by android app and also the Web app from same codeigniter setup. Below is my folder structure.

Controllers
    api
      -signupApi
  Signup

The signupApi is the actual rest API to be consumed by both android and web app. Signup is my actual signup screen controller where I have a form to be posted with user registration data to the signupApi.

if($_POST){
    // call the signupApi here and post the user registration data
}

If there is a signup POST request how do I call the signup API function which is there in the REST API controller and handle the request in the Signup controller. I did research about how to call a function from another controller function but could not get the right solution for me. Can anybody please suggest me how to proceed with it?

how do I call the signup API function which is there in the REST API controller and handle the request in the Signup controller

Sounds like you want to call a method in one controller from another controller. Most MVC frameworks (including CodeIgniter) intend a controller to only handle the request itself.

If you have logic that you need both controllers to implement, you'll want (and need) to put that logic into a model. CodeIgniter does initially give the impression that models are only for Database ORM interactions, but they are also the appropriate option for most shared logic you'll deal with.

If both methods are taking the exact same request in the same structure, merge the methods and handle the API vs Web App output based on the contents of those requests, or pass an additional parameter to reflect out the data should be output.

cURL is the most flexible way to interact with a REST API as it was designed for exactly this sort of thing. You can set HTTP headers, HTTP parameters and plenty more. cURL to make a POST request

Here is an example:

function function_name()
{
    $username = 'admin';
    $password = '1234';

    // Set up and execute the curl process
    $curl_handle = curl_init();
    curl_setopt($curl_handle, CURLOPT_URL, 'http://localhost/site/index.php/example_api');
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl_handle, CURLOPT_POST, 1);
    curl_setopt($curl_handle, CURLOPT_POSTFIELDS, array(
        'name' => 'name',
        'email' => 'example@example.com'
    ));

    // Optional, delete this line if your API is open
    curl_setopt($curl_handle, CURLOPT_USERPWD, $username . ':' . $password);

    $buffer = curl_exec($curl_handle);
    curl_close($curl_handle);

    $result = json_decode($buffer);

    if(isset($result->status) && $result->status == 'success')
    {
        echo 'Record inserted successfully...';
    }

    else
    {
        echo 'Something has gone wrong';
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM