PHP-CI - API撰寫

  • 1875
  • 0
  • PHP
  • 2015-04-16

摘要:PHP-CI - API撰寫

先修改route.php

加入

$route['api/user/(:any)'] = 'api/user/$1';    

 

在來寫controller

\application\controllers\api\user.php

內容為


 'my name',
					'id' => 1);
		echo json_encode($data);
	}
}
?>

 

及專案資料夾,

加入

.htaccess


DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

記得虛擬目錄也要改

測試

http://localhost/api/user/getdata

 

取得參數

假設API網址為:http://www.xxx.com.tw/api/user/getInfo?id=?

則可使用

$id = $this->input->get('id', TRUE); 

取得參數資料

參考文件:

http://www.zixuephp.com/html/javascript/2014_09/1786.html

 

後續發生問題 - Unexpected token 

http://www.dotblogs.com.tw/newmonkey48/archive/2015/04/16/151072.aspx