Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在codeigniter控制器函数中调用RESTAPI_Php_Rest_Api_Codeigniter_Codeigniter 3 - Fatal编程技术网

Php 在codeigniter控制器函数中调用RESTAPI

Php 在codeigniter控制器函数中调用RESTAPI,php,rest,api,codeigniter,codeigniter-3,Php,Rest,Api,Codeigniter,Codeigniter 3,我已经在codeigniter中设置了RESTAPI,android应用程序和Web应用程序将使用这些API。下面是我的文件夹结构 Controllers api -signupApi Signup signupApi是android和web应用程序实际使用的RESTAPISignup是我实际的注册屏幕控制器,在这里我有一个表单,将用户注册数据发布到signupApi if($_POST){ // call the signupApi here and post

我已经在codeigniter中设置了RESTAPI,android应用程序和Web应用程序将使用这些API。下面是我的文件夹结构

Controllers
    api
      -signupApi
  Signup
signupApi是android和web应用程序实际使用的RESTAPISignup是我实际的注册屏幕控制器,在这里我有一个表单,将用户注册数据发布到signupApi

if($_POST){
    // call the signupApi here and post the user registration data
}
如果存在注册后请求,我如何调用REST API控制器中的注册API函数并在注册控制器中处理该请求。我曾研究过如何从另一个控制器函数调用函数,但未能为我找到正确的解决方案。有谁能告诉我怎么做吗

如何调用RESTAPI中的注册API函数 控制器,并在注册控制器中处理请求

听起来您想从另一个控制器调用一个控制器中的方法。大多数MVC框架(包括CodeIgniter)都希望控制器只处理请求本身

如果您有需要两个控制器实现的逻辑,那么您需要(并且需要)将该逻辑放入模型中。CodeIgniter最初给人的印象是模型只用于数据库ORM交互,但它们也是您将要处理的大多数共享逻辑的合适选项


如果两个方法在相同的结构中接受完全相同的请求,则合并这些方法并根据这些请求的内容处理API vs Web App输出,或者传递一个额外的参数来反映应该输出的数据。

cURL是与restapi交互的最灵活的方式,因为它正是为这类事情而设计的。您可以设置HTTP头、HTTP参数等等。提交一个POST请求

以下是一个例子:

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

这有用吗?不完全是。。我需要一种方法来调用API控制器中存在的API端点及其所需的数据。我也不想将RESTAPI与PHP控制器逻辑混为一谈,这样android应用程序就可以盲目地使用该API,代码也保持干净。