Codeigniter ilovasida API integratsiyasi

CodeIgniter da ishlab chiqilgan CRM loyihamda MyOperator dan APIni integratsiya qilishga harakat qilaman. Buning uchun ular menga ba'zi kodlarni taqdim qilishadi, lekin bu kodni o'z arizamda ishlatishga harakat qilsam, menga JSON

404 sahifa topilmadi xato

Quyidagilarni taqdim etgan kod:

<?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();

Ushbu kod to'g'ridan-to'g'ri ushbu kodni ishlatishim kerak bo'lsa, kerakli chiqishni beradi, lekin men bu kodni ishlatadigan joyni tekshiruvchi qismida va qaysi qismda CodeIgniter ko'rinishida ishlatilishini shovqinlayman. Har qanday yordam oldindan xush kelibsiz.

3
Kechirasiz, shunchaki rahmat aytdim va ma'lumot olish uchun Post so'rovini ishlatadi.
qo'shib qo'ydi muallif Ganesh Aher, manba
@Nguyen Thanh Post so'rovini ishlatadi.
qo'shib qo'ydi muallif Ganesh Aher, manba
@ kishor10d Ular CORSga ruxsat berishadi
qo'shib qo'ydi muallif Ganesh Aher, manba
Codeigniter xotirjamlikdagi api kutubxonasini bu yerdan bog'lashga harakat qiling github.com/chriskacerguis/codeigniter-restserver
qo'shib qo'ydi muallif Hek mat, manba
Sizga yordam berishdan xursandman.
qo'shib qo'ydi muallif Nguyen Thanh, manba
Sizga yordam berishdan xursandman.
qo'shib qo'ydi muallif Nguyen Thanh, manba
@GaneshAher nima demoqchisiz? Men tushunmayman.
qo'shib qo'ydi muallif Nguyen Thanh, manba
Skriptingizni bajarishda olingan javob quyidagicha: {"status": "error", "message": "authorized request", "code": 404}
qo'shib qo'ydi muallif Nguyen Thanh, manba
CORS, men sizning CORS sozlamalaringizga qarashingiz kerak deb o'ylayman. Agar ular CORga ruxsat bermagan bo'lsa, api-ni cross-url (external-url) dan chaqira olmaydi.
qo'shib qo'ydi muallif kishor10d, manba

6 javoblar

Men CodeIgniter mutaxassisi emasman, lekin hujjatlarga va ba'zi bir havolalarga (masalan, bu va bu ) quyidagi kabi o'z kutubxonangizni yaratishingiz mumkin:

<?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";
    }

}

?>

Shundan so'ng siz kutubxonani quyidagi kabi boshqaruvchidan olishingiz mumkin:

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

?>

Umid qilamanki bu yordam!

2
qo'shib qo'ydi
Yordamingiz uchun tashakkur, lekin bo'sh chiqdi beradi.
qo'shib qo'ydi muallif Ganesh Aher, manba
Ha, hazratlari, men buni o'zgartirganman.
qo'shib qo'ydi muallif Ganesh Aher, manba
Kundalik faylimda hech narsa yo'q va butun ekran bo'sh (oq).
qo'shib qo'ydi muallif Ganesh Aher, manba
Ha, men ham buni qildim.
qo'shib qo'ydi muallif Ganesh Aher, manba
qo'shib qo'ydi muallif Mathieu Dormeval, manba
Ushbu qatorni o'zgartirishga harakat qiling: $ this-> load-> library ('operator'); bu bilan: $ this-> load-> library ('myoperator');
qo'shib qo'ydi muallif Mathieu Dormeval, manba
Kechirasiz, lekin tokenni to'g'ri qiymatga o'zgartirdingizmi? Siz kodni igniter versiyangiz nima? Mahalliy mashinamda harakat qilaman ...
qo'shib qo'ydi muallif Mathieu Dormeval, manba
Sizning ismingiz kim deb ataladi, PHP log xato faylini joylashtirasizmi? Xatolarimni olish uchun xatimni yangiladim.
qo'shib qo'ydi muallif Mathieu Dormeval, manba

Men CodeIgniter mutaxassisi emasman, lekin hujjatlarga va ba'zi bir havolalarga (masalan, bu va bu ) quyidagi kabi o'z kutubxonangizni yaratishingiz mumkin:

<?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";
    }

}

?>

Shundan so'ng siz kutubxonani quyidagi kabi boshqaruvchidan olishingiz mumkin:

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

?>

Umid qilamanki bu yordam!

2
qo'shib qo'ydi
Ha, hazratlari, men buni o'zgartirganman.
qo'shib qo'ydi muallif Ganesh Aher, manba
Kundalik faylimda hech narsa yo'q va butun ekran bo'sh (oq).
qo'shib qo'ydi muallif Ganesh Aher, manba
Ha, men ham buni qildim.
qo'shib qo'ydi muallif Ganesh Aher, manba
Yordamingiz uchun tashakkur, lekin bo'sh chiqdi beradi.
qo'shib qo'ydi muallif Ganesh Aher, manba
qo'shib qo'ydi muallif Mathieu Dormeval, manba
Ushbu qatorni o'zgartirishga harakat qiling: $ this-> load-> library ('operator'); bu bilan: $ this-> load-> library ('myoperator');
qo'shib qo'ydi muallif Mathieu Dormeval, manba
Kechirasiz, lekin tokenni to'g'ri qiymatga o'zgartirdingizmi? Siz kodni igniter versiyangiz nima? Mahalliy mashinamda harakat qilaman ...
qo'shib qo'ydi muallif Mathieu Dormeval, manba
Sizning ismingiz kim deb ataladi, PHP log xato faylini joylashtirasizmi? Xatolarimni olish uchun xatimni yangiladim.
qo'shib qo'ydi muallif Mathieu Dormeval, manba

Ushbu kodni bevosita ishlataman, lekin bu kodni qaerda ishlatishni, qaysi qismi tekshiruvchi va qaysi qismini CodeIgniter ichida ko'rish kerak deb shovqin qilaman.

CodeIgniter dasturining strukturasida tezkor astar kabi tartibda ishlaydi. Standart CI 3.1.3 versiyasi quyidagicha:

.
├── 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. In your case, the "Myoperator" library should be placed in application/libraries. Your controller will reside in application/controllers and views in application/views.

In your final Myoperator.php file, you'll need to remove the last two lines that you have in your example. 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');

Keyin kutubxonani shunday chaqiring:

$this->myoperator->run();

This is the CodeIgniter equivalent of those last 2 lines, $Class = new Myoperator(); and $Class->run();

2
qo'shib qo'ydi

Ilova/kutubxonalar/ yo'nalishi bo'yicha o'z kutubxonangizni yarating Va quyidagi kodni joylashtiring:

 <?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";
} } ?>

Shundan so'ng, kutubxonani quyidagi kabi boshqarishingiz mumkin:

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

Xatolarni ko'rib chiqish:

public function index(){
   try {
       $this->load->library('operator');  
       $this->operator->run();
   } catch (Exception $e) {
       var_dump($e->getMessage());
   }}
1
qo'shib qo'ydi
Replay uchun rahmat, men allaqachon qilaman. Lekin hozir men uchun jsonli fayllardan qanday qadriyatlar olish kerakligi.
qo'shib qo'ydi muallif Ganesh Aher, manba
Ishlash funktsiyasi ichidagi kutubxona faylida bu kabi json qiymatini qaytaradi: return json_encode ($ result);
qo'shib qo'ydi muallif Amarjeet Chahal, manba

Agar ular biron bir kutubxonani bergan bo'lsa, siz uni nazoratchi faylida kutubxonani o'z ichiga olsangiz, undan to'g'ridan-to'g'ri o'z funksiyasini chaqirasiz. Agar ular sizga kodni taqdim qilsalar, o'z urlini chaqirish uchun o'zingizning api faylini yaratishingiz kerak .Just kod kodni web/application/libraries/Operator.php (kutubxonaning nomini olgan holda) ga joylashtiring.

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

}

Keyin sizning maxsus tekshiruvgichingizda api-ni qo'shing va nazorat-o'lchov asbobini uni kengaytiring. Keyin u api funksiyasini avtomatik ravishda chaqiradi.

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

Men api ishini bilmayman, lekin siz bu usulni sinab ko'rishingiz mumkin. Men ishlayman deb umid qilaman.

1
qo'shib qo'ydi

Ushbu kodni ishlatib javobimga javob berdim: Aslida Admin_controller kodini mening Controller sinfimga qo'shishni unutgan edim.

Mening kutubxonam fayli:

<?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";
    }
}

Ushbu id tekshiruvi kodi:

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

tahrirlangan:

Bu men uchun qidiradigan kodni json formatida taqdim etadi.

0
qo'shib qo'ydi
Aslida yuqoridagi kod menga chiqishni beradi. Kechirasiz, mening javobimni yangilamayman.
qo'shib qo'ydi muallif Ganesh Aher, manba
Javobni qabul qilmaslik juda yoqimli emas (ko'pincha ne'mat uchun), men seni to'g'ri yo'lga qo'yganman!
qo'shib qo'ydi muallif Mathieu Dormeval, manba
json_decode() qo'ng'iroqni qaerda qilyapsiz? $ result ga murojaat qiladigan kutubxona faylida ko'raman. Bu erda json_decode() ni ishlatmoqdamisiz? Agar shunday bo'lsa, kutubxonangizdagi run() usulidan $ result (yoki json_decode ($ result) ) ni qaytarishingiz kerak. $ data ['m'] uchun tekshiruvchining indeks usulidagi qaytarish qiymati. Keyin ko'rinishni $ this-> load-> view ("administrator/myoperator/view", $ data) orqali kiritishingiz mumkin;
qo'shib qo'ydi muallif Nigel Tufnel, manba
PhP |BotsUz
PhP |BotsUz
93 ishtirokchilar

Phpni o'rganishni Hohlasangiz https://t.me/joinchat/AAAAAE-KRc5dd5tPMmGmWA A'zo bo'lin