如何以及在哪里投放笨文件夹中的API?
如果你的网站有一个API,然后把合适的地方是在你的应用程序/文件夹的控制器控制一些,这就是你如何从外部API调用响应。 例子:
class Api extends CI_Controller { //Your code goes here }
如果你想使用你想从反应的另一个API,好的做法是创建应用程序/库/ 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类中完成。 我希望这对你有帮助!
感谢您的帮助。我有另一个问题。如何从我的控制器中调用application/libraries/Some_Api.php中的API函数?谢谢! – user634850 2011-04-09 20:12:53
我编辑了我的答案! – zokibtmkd 2011-04-09 20:52:09
我一直有一个关于这个问题为好。我会告诉你我的方法,并且我渴望得到反馈。当包括别人的API我在模型
例如
class Model extends CI_Model{
.
.
.
function writeLocally{
//do some stuff
$this->apiWrite();
}
private function apiWrite{
//api write to 3rd party
}
}
我猜测这是不是基于上述答案的最佳途径,但反正这是我的两分钱包括它。
你的问题是混乱的。您是否想要编写自己的API或实现其他应用程序的API以便在您的应用程序中使用? – njbair 2011-04-09 21:45:46