回答
如果你的網站有一個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. 如何追加到Xcode 4中的文件以及文件應放在哪裏?
- 2. 在哪裏把jar文件放在apache中?以及如何從Perl訪問它?
- 3. 在哪裏以及如何在node.js中提供文件
- 4. 在哪裏投放Clojure.Spec?
- 5. 什麼是'.R'文件夾以及在哪裏查找它?
- 6. 除了Addins文件夾,VS插件在哪裏(以及如何)部署?
- 7. 如何在nginx中使用.htaccess以及在哪裏放置它?
- 8. 哪裏把屬性文件放在tomcat的config文件夾中
- 9. 哪裏可以放置joomla組件的css文件夾
- 10. 我在哪裏可以將cpanel備份文件放在www文件夾中?
- 11. 在哪裏以及如何在C
- 12. 我在哪裏可以放置JQUERY UI主題文件夾?
- 13. Grails - 在哪裏放置測試文件以及如何加載它們
- 14. 把文件放在哪裏以及如何編譯和運行它?
- 15. 我在哪裏以及如何安裝twitter的Python API?
- 16. 「Developer」文件夾在哪裏?
- 17. localhost文件夾在哪裏
- 18. 在哪裏執行onClickListener以及如何?
- 19. 在哪裏以及如何使用Page.RegisterRequiresViewStateEncryption()?
- 20. 如何以及在哪裏使用instantiateViewControllerWithIdentifier
- 21. EmberJs - 如何以及在哪裏學習?
- 22. UWP(C#) - 在哪裏以及如何保存和訪問文件?
- 23. 編輯:在哪裏以及如何下載臨時文件?
- 24. 如何以及在哪裏下載itextpdf jar文件
- 25. GAE blobstore在哪裏以及如何存儲文件(或blob)?
- 26. 該文件在哪裏以及如何被gzip壓縮?
- 27. 在哪裏放置夾具?
- 28. Windows中的「〜/ .gradle」文件夾在哪裏?
- 29. XAMPP中的wordpress文件夾在哪裏?
- 30. Docker中的workdir文件夾在哪裏?
你的問題是混亂的。您是否想要編寫自己的API或實現其他應用程序的API以便在您的應用程序中使用? – njbair 2011-04-09 21:45:46