回答
如果你的网站有一个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
}
}
我猜测这是不是基于上述答案的最佳途径,但反正这是我的两分钱包括它。
- 1. 在哪里把jar文件放在apache中?以及如何从Perl访问它?
- 2. 在哪里以及如何在node.js中提供文件
- 3. 在哪里投放Clojure.Spec?
- 4. 什么是'.R'文件夹以及在哪里查找它?
- 5. 除了Addins文件夹,VS插件在哪里(以及如何)部署?
- 6. 如何在nginx中使用.htaccess以及在哪里放置它?
- 7. 哪里把属性文件放在tomcat的config文件夹中
- 8. 我在哪里可以将cpanel备份文件放在www文件夹中?
- 9. 在哪里以及如何在C
- 10. 我在哪里可以放置JQUERY UI主题文件夹?
- 11. 我在哪里以及如何安装twitter的Python API?
- 12. localhost文件夹在哪里
- 13. “Developer”文件夹在哪里?
- 14. 如何以及在哪里下载itextpdf jar文件
- 15. UWP(C#) - 在哪里以及如何保存和访问文件?
- 16. 编辑:在哪里以及如何下载临时文件?
- 17. 在哪里以及如何使用Page.RegisterRequiresViewStateEncryption()?
- 18. 如何以及在哪里使用instantiateViewControllerWithIdentifier
- 19. 如何以及在哪里使用Transformations.switchMap?
- 20. EmberJs - 如何以及在哪里学习?
- 21. 在哪里执行onClickListener以及如何?
- 22. XAMPP中的wordpress文件夹在哪里?
- 23. Docker中的workdir文件夹在哪里?
- 24. Windows中的“〜/ .gradle”文件夹在哪里?
- 25. 清单笨文件,子文件夹及其文件的文件笨
- 26. 笨 - 在哪里放置脚本与
- 27. 我在哪里把图像放在eclipse资源文件夹中?
- 28. FileSystem API:位于哪里的文件和文件夹
- 29. 默认软件包是如何创建的以及在哪里?
- 30. 如何以及在哪里创建aidl文件在eclipse中使用android
你的问题是混乱的。您是否想要编写自己的API或实现其他应用程序的API以便在您的应用程序中使用? – njbair 2011-04-09 21:45:46