简体   繁体   English

Codeigniter应用程序中的API集成

[英]API integration in Codeigniter application

I'm trying to integrate API from MyOperator , in my CRM project, which is developed in CodeIgniter . 我正在尝试在我的CRM项目中集成来自MyOperator API,这是在CodeIgniter开发的。 For that they provide me some code, but when I tried to use that code in my application, it give me JSON data with 为此,他们为我提供了一些代码,但是当我尝试在我的应用程序中使用该代码时,它会为我提供JSON数据

404 page not found error 找不到404页面错误

Here is the code that they provided : 这是他们提供的代码:

<?php                                                                                   

/**
 * @description : Library to access MyOperator Public API
 */
Class Myoperator {

    protected $developers_url = 'https://developers.myoperator.co/';
    protected $token = 'XXXXXXXXX';

    function __construct() {

    }

    public function run() {
        # request for Logs
        $url = $this->developers_url . 'search';
        $fields = array("token" => $this->token);
        $result = $this->_post_api($fields, $url);

        $this->log("result");
        $this->log($result);
    }

    private function _post_api(Array $fields, $url) {
        try {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_TIMEOUT, 30);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
            $result = curl_exec($ch);
        } catch (Exception $e) {
            return false;
        }
        $this->log("url");
        $this->log($url);
        $this->log("fields");
        $this->log($fields);
        curl_close($ch);
        if ($result)
            return $result;
        else
            return false;
    }

    private function log($message) {
        print_r($message);
        echo "\n";
    }

}


$Class = new Myoperator();
$Class->run();

This code gives me desired output if I run this code directly, but I'm confuse that where to use this code, which part is in controller and which part is in view in CodeIgniter . 如果我直接运行这段代码,这段代码给了我想要的输出,但我很困惑,在哪里使用这段代码,哪一部分在控制器中,哪一部分在CodeIgniter中查看。 Any kind of help is welcome, thanks in advance. 欢迎任何形式的帮助,提前谢谢。

I'm not a specialist of CodeIgniter, but after having a look to the documentation, and some links (like this and this ) you can create your own library like this : 我不是CodeIgniter的专家,但在查看了文档和一些链接(比如这个这个 )后,您可以创建自己的库,如下所示:

<?php                                                                                   

defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * @description : Library to access MyOperator Public API
 */
Class Myoperator {

    protected $developers_url = 'https://developers.myoperator.co/';
    protected $token = 'XXXXXXXXX';

    function __construct() {

    }

    public function run() {
        # request for Logs
        $url = $this->developers_url . 'search';
        $fields = array("token" => $this->token);
        $result = $this->_post_api($fields, $url);

        $this->log("result");
        $this->log($result);
    }

    private function _post_api(Array $fields, $url) {
        try {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_TIMEOUT, 30);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
            $result = curl_exec($ch);
        } catch (Exception $e) {
            return false;
        }
        $this->log("url");
        $this->log($url);
        $this->log("fields");
        $this->log($fields);
        curl_close($ch);
        if ($result)
            return $result;
        else
            return false;
    }

    private function log($message) {
        print_r($message);
        echo "\n";
    }

}

?>

After that, you can get the library from the controller, like that : 之后,您可以从控制器获取库,如下所示:

<?php

class Operator extends CI_Controller 
{
   public function index()
   {
       $this->load->library('operator');  
       $this->operator->run();
   }
}

?>

Edit (Error handling) 编辑 (错误处理)

<?php

class Operator extends CI_Controller 
{
   public function index()
   {
       try {
           $this->load->library('operator');  
           $this->operator->run();
       } catch (Exception $e) {
           var_dump($e->getMessage());
       }
   }
}

?>

Hope this helps ! 希望这可以帮助 !

I run this code directly, but I'm confuse that where to use this code, which part is in controller and which part is in view in CodeIgniter. 我直接运行这个代码,但是我很困惑,在哪里使用这个代码,哪个部分在控制器中,哪个部分在CodeIgniter中查看。

It sounds like a quick primer on the structure of a CodeIgniter application is in order. 这听起来像是CodeIgniter应用程序结构的快速入门。 A default CI 3.1.3 release has this structure: 默认的CI 3.1.3版本具有以下结构:

.
├── application/
├── composer.json
├── contributing.md
├── index.php
├── license.txt
├── readme.rst
├── system/
└── user_guide/

Your application, including your supplied library , will reside inside the application directory. 您的应用程序( 包括提供的库 )将驻留在application目录中。 In your case, the "Myoperator" library should be placed in application/libraries . 在您的情况下,“Myoperator”库应放在application/libraries Your controller will reside in application/controllers and views in application/views . 您的控制器将驻留在application/controllersapplication/views

In your final Myoperator.php file, you'll need to remove the last two lines that you have in your example. 在最终的Myoperator.php文件中,您需要删除示例中的最后两行。 Those will be replaced by their equivalents in your controller. 这些将由您在控制器中的等效物替换。

In your controller , you simply need to load the library like this: 在您的控制器中 ,您只需要像这样加载库:

$this->load->library('myoperator');

Then invoke the library like this: 然后像这样调用库:

$this->myoperator->run();

This is the CodeIgniter equivalent of those last 2 lines, $Class = new Myoperator(); 这是CodeIgniter等效的最后两行, $Class = new Myoperator(); and $Class->run(); $Class->run();

In case they have given any library then you use that by including that library in your controller file then directly call their function. 如果他们给了任何库,那么你通过在控制器文件中包含该库来使用它,然后直接调用它们的函数。 If they have provided you a code then you have to create your own api file to call their url .Just code paste the code into the web/application/libraries/Operator.php(assuming the name of library). 如果他们为您提供了代码,那么您必须创建自己的api文件来调用他们的url。代码将代码粘贴到web / application / libraries / Operator.php(假设库的名称)。

Operator.php Operator.php

<?php                                                                                   

/**
 * @description : Library to access MyOperator Public API
 */
Class Myoperator {

    protected $developers_url = 'https://developers.myoperator.co/';
    protected $token = 'XXXXXXXXX';

    function __construct() {

    }

    public function run() {
        # request for Logs
        $url = $this->developers_url . 'search';
        $fields = array("token" => $this->token);
        $result = $this->_post_api($fields, $url);

        $this->log("result");
        $this->log($result);
    }

    private function _post_api(Array $fields, $url) {
        try {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_TIMEOUT, 30);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
            $result = curl_exec($ch);
        } catch (Exception $e) {
            return false;
        }
        $this->log("url");
        $this->log($url);
        $this->log("fields");
        $this->log($fields);
        curl_close($ch);
        if ($result)
            return $result;
        else
            return false;
    }

    private function log($message) {
        print_r($message);
        echo "\n";
    }

}

Then in your custom controller include the api and extend the controller with it.And then call the function of that api automatically. 然后在你的自定义控制器中包含api并用它扩展控制器。然后自动调用该api的功能。

require('application/libraries/Operator.php');
class Users extends Operator
{
    public function __Construct()
    {
       $this->run(); /*You can call the function directly using $this*/
    }
}

I don't know the working of the api but the method you can try this.I hope it will work. 我不知道api的工作方式,但你可以试试这个方法。我希望它能运作。

Create your own library in the following direction Application/libraries/ And paste the following code: 按以下方向创建自己的库Application / libraries /并粘贴以下代码:

 <?php defined('BASEPATH') OR exit('No direct script access allowed'); Class Myoperator {

protected $developers_url = 'https://developers.myoperator.co/';
protected $token = 'XXXXXXXXX';

function __construct() {

}

public function run() {
    # request for Logs
    $url = $this->developers_url . 'search';
    $fields = array("token" => $this->token);
    $result = $this->_post_api($fields, $url);

    $this->log("result");
    $this->log($result);
}

private function _post_api(Array $fields, $url) {
    try {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        $result = curl_exec($ch);
    } catch (Exception $e) {
        return false;
    }
    $this->log("url");
    $this->log($url);
    $this->log("fields");
    $this->log($fields);
    curl_close($ch);
    if ($result)
        return $result;
    else
        return false;
}

private function log($message) {
    print_r($message);
    echo "\n";
} } ?>

After that, you can use the library in the controller, like that : 之后,您可以在控制器中使用库,如下所示:

  public function index() {
   $this->load->library('operator');  
   $this->operator->run();}

Error handling: 错误处理:

public function index(){
   try {
       $this->load->library('operator');  
       $this->operator->run();
   } catch (Exception $e) {
       var_dump($e->getMessage());
   }}

By using this code I got my answer, Actually I forgot to include my Admin_controller in my Controller class. 通过使用此代码我得到了答案,实际上我忘了在Controller类中包含我的Admin_controller

My library file : 我的库文件:

<?php                                                                                   

defined('BASEPATH') OR exit('No direct script access allowed');

//require APPPATH. 'core/Admin_controller.php';

/**
 * @description : Library to access MyOperator Public API
 */
Class MY_Operator {

    protected $developers_url = 'https://developers.myoperator.co/';
    protected $token = 'XXXXXXXXXX';

    function __construct() {

    }

    public function run() {
        # request for Logs
        $url = $this->developers_url . 'search';
        $fields = array("token" => $this->token);
        $result = $this->_post_api($fields, $url);

        $this->log("result");
        $this->log($result);
    }

    private function _post_api(Array $fields, $url) {
        try {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_TIMEOUT, 30);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
            $result = curl_exec($ch);
        } catch (Exception $e) {
            return false;
        }
        $this->log("url");
        $this->log($url);
        $this->log("fields");
        $this->log($fields);
        curl_close($ch);
        if ($result)
            return $result;
        else
            return false;
    }

    private function log($message) {
        print_r($message);
        echo "\n";
    }
}

This id Controller code : 这个id控制器代码:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

require APPPATH. 'core/Admin_controller.php';
//Added above line to get output
class Myoperator extends Admin_controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->library('MY_Operator');
        //$this->load->model('home_model');
    }

    public function index()
   {
       try {

            $this->my_operator->run();
       } catch (Exception $e) {
           var_dump($e->getMessage());
       }

       $this->load->view('admin/myoperator/view');
   }
}

Edited : 编辑:

This is the code that gives me output, in json format, which is I'm looking for. 这是以json格式给出输出的代码,这是我正在寻找的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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