2011-04-09 2 views

답변

1

사이트가 API 인 경우 응용 프로그램/컨트롤러 폴더에 적절한 컨트롤러를 배치하면 외부에서 API 호출에 응답하는 방법이됩니다. 예 : 당신은 당신이 응답을 얻으려면 다른 API를 사용하려면

 
class Api extends CI_Controller 
{ 
    //Your code goes here 
} 

좋은 방법은 외부로 요청과 응답을 처리하는 응용 프로그램/라이브러리/Some_Api.php에서 클래스를 생성하는 것입니다 API를 사용하면 필요에 따라 컨트롤러, 모델 등의 어딘가에서 호출해야합니다. 이것에 대한

편집 예는 다음과 같습니다

 
class Myapi extends CI_Controller 
{ 
    public function __construct() 
    { 
     parent::__construct(); 
    } 

    //Here goes your function: 
    public function api_call() 
    { 
     //Some params that Some_Api class will need (if needed of course) 
     $params = array('url' => 'https://checkout.google.com', 'username' => 'your_username'); 

     //Include the library 
     require_once(APPPATH.'libraries/Some_Api.php'); 

     //Create instance of it 
     $api = new Some_Api($params); 

     //Call the external API and collect the response. 
     //In most cases the response is XML and you need to process it as you need it. 
     $response = $api->call_some_api_method(); 

     //Process the response here. 
     //Save to database/file, send emails, do whatever you need to. 
    } 
} 

난 그냥 프로세스가 일반적으로가는 방법을 설명하는 몇 가지 사소한 예를 사용하고 있습니다. 대부분의 작업은 Some_Api 클래스에서 수행해야합니다. 이 도움이 되었기를 바랍니다.

+0

도와 주셔서 감사합니다. 다른 질문이 있습니다. 내 컨트롤러에서 application/libraries/Some_Api.php의 API 함수를 호출하는 방법? 감사! – user634850

+0

나는 나의 대답을 편집했다! – zokibtmkd

0

나는 이것에 관해서도 항상 질문을 가지고있다. 나는 당신에게 나의 접근 방식을 말할 것이고 나는 피드백을 얻고 싶어한다. 누군가 다른 사람의 API를 포함 할 때 나는 예를

class Model extends CI_Model{ 

. 
. 
. 

    function writeLocally{ 

    //do some stuff 


    $this->apiWrite(); 
    } 

    private function apiWrite{ 

    //api write to 3rd party 
    } 


} 

나는이 내 두 센트 어쨌든 제일 위의 답변에 따라 접근 방식이 아닌 추측하고의 모델

에 포함되어있다.

관련 문제