hi i developing android app u sing yii restful api
i gonna add Android ListView with Load More Button( u
like this tutorial
i want sent data using RESTFUL like in this tutorial(XML list)
how make it
[color="#FF0000"]i use this web service[/color]
http://www.yiiframework.com/wiki/175/how-to-create-a-rest-api/
this is my restful
<?php
/**
-
ApiController class file
-
@author Joachim Werner <joachim.werner@diggin-data.de>
*/
/**
-
ApiController
-
@uses Controller
-
@author Joachim Werner <joachim.werner@diggin-data.de>
-
@author
-
@see http://www.gen-x-design.com/archives/making-restful-requests-in-php/
-
@license (tbd)
*/
class ApiController extends Controller
{
// {{{ *** Members ***
/**
* Key which has to be in HTTP USERNAME and PASSWORD headers
*/
Const APPLICATION_ID = 'ASCCPE';
private $format = 'json';
// }}}
// {{{ filters
/**
* @return array action filters
*/
public function filters()
{
return array();
} // }}}
// {{{ *** Actions ***
// {{{ actionIndex
public function actionIndex()
{
echo CJSON::encode(array(1, 2, 3));
} // }}}
// {{{ actionList
public function actionList()
{
//$this->_checkAuth();
switch($_GET['model'])
{
case 'places': // {{{
//get list of places related with city_id in places table
if (isset($_GET['cityid']))
{
$Criteria = new CDbCriteria();
$Criteria->condition = sprintf("city_id = %s", $_GET['cityid']);
$models = Cities::model()->findAll($Criteria);
}else{
$models = Places::model()->findAll();
}
break; // }}}
case ‘cities’: // {{{
//get list of places related with city_id in cities table
if (isset($_GET['cityid']))
{
$Criteria = new CDbCriteria();
$Criteria->condition = sprintf("city_id = %s", $_GET['cityid']);
$models = Places::model()->findAll($Criteria);
}
$models = Cities::model()->findby($Criteria);
}
else{
$models = Cities::model()->findAll();
}
break; // }}}
case ‘hotels’: // {{{
//get list of places related with hotel_id in hotels table
if (isset($_GET['hotelid']))
{
$Criteria = new CDbCriteria();
$Criteria->condition = sprintf("hotel_id = %s", $_GET['hotelid']);
$models = Places::model()->findAll($Criteria);
}
else{
$models = Hotels::model()->findAll();
}
break; // }}}
case ‘Category’: // {{{
//get list of type_places related with id in Catagory table
if (isset($_GET['Categoryid']))
{
$Criteria = new CDbCriteria();
$Criteria->condition = sprintf("catagory_id = %s", $_GET['Categoryid']);
$models = TypePlaces::model()->findAll($Criteria);
}else{
$models = Category::model()->findAll();
}
break; // }}}
case ‘Country’: // {{{
$models = Country::model()->findAll();
break; // }}}
case ‘Provinces’: // {{{
$models = Provinces::model()->findAll();
break; // }}}
case ‘Rate’: // {{{
$models = Rate::model()->findAll();
break; // }}}
case ‘TypeHotel’: // {{{
$models = TypeHotel::model()->findAll();
break; // }}}
case ‘TypePlaces’: // {{{
//
//get list of places related with id in TypePlaces table
if (isset($_GET['typeplacesid']))
{
$Criteria = new CDbCriteria();
$Criteria->condition = sprintf("id = %s", $_GET['typeplacesid']);
$models = Places::model()->findAll($Criteria);
}
else{
$models = TypePlaces::model()->findAll();
}
break; // }}}
default: // {{{
$this->_sendResponse(501, sprintf('Error: Mode <b>list</b> is not implemented for model <b>%s</b>',$_GET['model']) );
exit; // }}}
}
if(is_null($models)) {
$this->_sendResponse(200, sprintf('No items where found for model <b>%s</b>', $_GET['model']) );
} else {
$rows = array();
foreach($models as $model)
$rows[] = $model->attributes;
$this->_sendResponse(200, CJSON::encode($rows),"application/json");
}
} // }}}
// {{{ actionView
/* Shows a single item
*
* @access public
* @return void
*/
public function actionView()
{
//$this->_checkAuth();
// Check if id was submitted via GET
if(!isset($_GET['id']))
$this->_sendResponse(500, 'Error: Parameter <b>id</b> is missing' );
switch($_GET['model'])
{
// Find respective model
case 'places': // {{{
$model = Places::model()->findByPk($_GET['id']);
break; // }}}
case 'cities': // {{{
$model = Cities::model()->findByPk($_GET['id']);
break; // }}}
case 'hotels': // {{{
$model = Hotels::model()->findByPk($_GET['id']);
break; // }}}
case 'Category': // {{{
$model = Category::model()->findByPk($_GET['id']);
break; // }}}
case 'Country': // {{{
$model = Country::model()->findByPk($_GET['id']);
break; // }}}
case 'Provinces': // {{{
$model = Provinces::model()->findByPk($_GET['id']);
break; // }}}
case 'Rate': // {{{
$model = Rate::model()->findByPk($_GET['id']);
break; // }}}
case 'TypeHotel': // {{{
$model = TypeHotel::model()->findByPk($_GET['id']);
break; // }}}
case 'TypePlaces': // {{{
$model = TypePlaces::model()->findByPk($_GET['id']);
break; // }}}
default: // {{{
$this->_sendResponse(501, sprintf('Mode <b>view</b> is not implemented for model <b>%s</b>',$_GET['model']) );
exit; // }}}
}
if(is_null($model)) {
$this->_sendResponse(404, 'No Item found with id '.$_GET['id']);
} else {
$this->_sendResponse(200, $this->_getObjectEncoded($_GET['model'], $model->attributes));
}
} // }}}
// {{{ actionCreate
/**
* Creates a new item
*
* @access public
* @return void
*/
public function actionCreate()
{
//$this->_checkAuth();
switch($_GET['model'])
{
// Get an instance of the respective model
case 'places': // {{{
$model = new Places;
break; // }}}
case 'cities': // {{{
$model = new Cities;
break; // }}}
case 'hotels': // {{{
$model = new Hotels;
break; // }}}
case 'Category': // {{{
$model = new Category;
break; // }}}
case 'Country': // {{{
$model = new Country;
break; // }}}
case 'Provinces': // {{{
$model = new Provinces;
break; // }}}
case 'Rate': // {{{
$model = new Rate;
break; // }}}
case 'TypeHotel': // {{{
$model = new TypeHotel;
break; // }}}
case 'TypePlaces': // {{{
$model = new TypePlaces;
break; // }}}
default: // {{{
$this->_sendResponse(501, sprintf('Mode <b>create</b> is not implemented for model <b>%s</b>',$_GET['model']) );
exit; // }}}
}
// Try to assign POST values to attributes
foreach($_POST as $var=>$value) {
// Does the model have this attribute?
if($model->hasAttribute($var)) {
$model->$var = $value;
} else {
// No, raise an error
$this->_sendResponse(500, sprintf('Parameter <b>%s</b> is not allowed for model <b>%s</b>', $var, $_GET['model']) );
}
}
// Try to save the model
if($model->save()) {
// Saving was OK
$this->_sendResponse(200, $this->_getObjectEncoded($_GET['model'], $model->attributes) );
} else {
// Errors occurred
$msg = "<h1>Error</h1>";
$msg .= sprintf("Couldn't create model <b>%s</b>", $_GET['model']);
$msg .= "<ul>";
foreach($model->errors as $attribute=>$attr_errors) {
$msg .= "<li>Attribute: $attribute</li>";
$msg .= "<ul>";
foreach($attr_errors as $attr_error) {
$msg .= "<li>$attr_error</li>";
}
$msg .= "</ul>";
}
$msg .= "</ul>";
$this->_sendResponse(500, $msg );
}
var_dump($_REQUEST);
} // }}}
// {{{ actionUpdate
/**
* Update a single iten
*
* @access public
* @return void
*/
public function actionUpdate()
{
$this->_checkAuth();
// Get PUT parameters
parse_str(file_get_contents('php://input'), $put_vars);
switch($_GET['model'])
{
// Find respective model
case 'places': // {{{
$model = Places::model()->findByPk($_GET['id']);
break; // }}}
case 'cities': // {{{
$model = Cities::model()->findByPk($_GET['id']);
break; // }}}
case 'hotels': // {{{
$model = Hotels::model()->findByPk($_GET['id']);
break; // }}}
case 'Category': // {{{
$model = Category::model()->findByPk($_GET['id']);
break; // }}}
case 'Country': // {{{
$model = Country::model()->findByPk($_GET['id']);
break; // }}}
case 'Provinces': // {{{
$model = Provinces::model()->findByPk($_GET['id']);
break; // }}}
case 'Rate': // {{{
$model = Rate::model()->findByPk($_GET['id']);
break; // }}}
case 'TypeHotel': // {{{
$model = TypeHotel::model()->findByPk($_GET['id']);
break; // }}}
case 'TypePlaces': // {{{
$model = TypePlaces::model()->findByPk($_GET['id']);
break; // }}}
default: // {{{
$this->_sendResponse(501, sprintf('Error: Mode <b>update</b> is not implemented for model <b>%s</b>',$_GET['model']) );
exit; // }}}
}
if(is_null($model))
$this->_sendResponse(400, sprintf("Error: Didn't find any model <b>%s</b> with ID <b>%s</b>.",$_GET['model'], $_GET['id']) );
// Try to assign PUT parameters to attributes
foreach($put_vars as $var=>$value) {
// Does model have this attribute?
if($model->hasAttribute($var)) {
$model->$var = $value;
} else {
// No, raise error
$this->_sendResponse(500, sprintf('Parameter <b>%s</b> is not allowed for model <b>%s</b>', $var, $_GET['model']) );
}
}
// Try to save the model
if($model->save()) {
$this->_sendResponse(200, sprintf('The model <b>%s</b> with id <b>%s</b> has been updated.', $_GET['model'], $_GET['id']) );
} else {
$msg = "<h1>Error</h1>";
$msg .= sprintf("Couldn't update model <b>%s</b>", $_GET['model']);
$msg .= "<ul>";
foreach($model->errors as $attribute=>$attr_errors) {
$msg .= "<li>Attribute: $attribute</li>";
$msg .= "<ul>";
foreach($attr_errors as $attr_error) {
$msg .= "<li>$attr_error</li>";
}
$msg .= "</ul>";
}
$msg .= "</ul>";
$this->_sendResponse(500, $msg );
}
} // }}}
// {{{ actionDelete
/**
* Deletes a single item
*
* @access public
* @return void
*/
public function actionDelete()
{
//$this->_checkAuth();
switch($_GET['model'])
{
// Load the respective model
case 'places': // {{{
$model = Places::model()->findByPk($_GET['id']);
break; // }}}
case 'cities': // {{{
$model = Cities::model()->findByPk($_GET['id']);
break; // }}}
case 'hotels': // {{{
$model = Hotels::model()->findByPk($_GET['id']);
break; // }}}
case 'Category': // {{{
$model = Category::model()->findByPk($_GET['id']);
break; // }}}
case 'Country': // {{{
$model = Country::model()->findByPk($_GET['id']);
break; // }}}
case 'Provinces': // {{{
$model = Provinces::model()->findByPk($_GET['id']);
break; // }}}
case 'Rate': // {{{
$model = Rate::model()->findByPk($_GET['id']);
break; // }}}
case 'TypeHotel': // {{{
$model = TypeHotel::model()->findByPk($_GET['id']);
break; // }}}
case 'TypePlaces': // {{{
$model = TypePlaces::model()->findByPk($_GET['id']);
break; // }}}
default: // {{{
$this->_sendResponse(501, sprintf('Error: Mode <b>delete</b> is not implemented for model <b>%s</b>',$_GET['model']) );
exit; // }}}
}
// Was a model found?
if(is_null($model)) {
// No, raise an error
$this->_sendResponse(400, sprintf("Error: Didn't find any model <b>%s</b> with ID <b>%s</b>.",$_GET['model'], $_GET['id']) );
}
// Delete the model
$num = $model->delete();
if($num>0)
$this->_sendResponse(200, sprintf("Model <b>%s</b> with ID <b>%s</b> has been deleted.",$_GET['model'], $_GET['id']) );
else
$this->_sendResponse(500, sprintf("Error: Couldn't delete model <b>%s</b> with ID <b>%s</b>.",$_GET['model'], $_GET['id']) );
} // }}}
// }}} End Actions
// {{{ Other Methods
// {{{ _sendResponse
/**
* Sends the API response
*
* @param int $status
* @param string $body
* @param string $content_type
* @access private
* @return void
*/
private function _sendResponse($status = 200, $body = '', $content_type = 'text/html')
{
$status_header = 'HTTP/1.1 ' . $status . ' ' . $this->_getStatusCodeMessage($status);
// set the status
header($status_header);
// set the content type
header('Content-type: ' . $content_type);
// pages with body are easy
if($body != '')
{
// send the body
echo $body;
exit;
}
// we need to create the body if none is passed
else
{
// create some body messages
$message = '';
// this is purely optional, but makes the pages a little nicer to read
// for your users. Since you won't likely send a lot of different status codes,
// this also shouldn't be too ponderous to maintain
switch($status)
{
case 401:
$message = 'You must be authorized to view this page.';
break;
case 404:
$message = 'The requested URL ' . $_SERVER['REQUEST_URI'] . ' was not found.';
break;
case 500:
$message = 'The server encountered an error processing your request.';
break;
case 501:
$message = 'The requested method is not implemented.';
break;
}
// servers don't always have a signature turned on (this is an apache directive "ServerSignature On")
$signature = ($_SERVER['SERVER_SIGNATURE'] == '') ? $_SERVER['SERVER_SOFTWARE'] . ' Server at ' . $_SERVER['SERVER_NAME'] . ' Port ' . $_SERVER['SERVER_PORT'] : $_SERVER['SERVER_SIGNATURE'];
// this should be templatized in a real-world solution
$body = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>' . $status . ' ' . $this->_getStatusCodeMessage($status) . '</title>
</head>
<body>
<h1>' . $this->_getStatusCodeMessage($status) . '</h1>
<p>' . $message . '</p>
<hr />
<address>' . $signature . '</address>
</body>
</html>';
echo $body;
exit;
}
} // }}}
// {{{ _getStatusCodeMessage
/**
* Gets the message for a status code
*
* @param mixed $status
* @access private
* @return string
*/
private function _getStatusCodeMessage($status)
{
// these could be stored in a .ini file and loaded
// via parse_ini_file()... however, this will suffice
// for an example
$codes = Array(
100 => 'Continue',
101 => 'Switching Protocols',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
306 => '(Unused)',
307 => 'Temporary Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported'
);
return (isset($codes[$status])) ? $codes[$status] : '';
} // }}}
// {{{ _checkAuth
/**
* Checks if a request is authorized
*
* @access private
* @return void
*/
private function _checkAuth()
{
// Check if we have the USERNAME and PASSWORD HTTP headers set?
if(!(isset($_SERVER['HTTP_X_'.self::APPLICATION_ID.'_USERNAME']) and isset($_SERVER['HTTP_X_'.self::APPLICATION_ID.'_PASSWORD']))) {
// Error: Unauthorized
$this->_sendResponse(401);
}
$username = $_SERVER['HTTP_X_'.self::APPLICATION_ID.'_USERNAME'];
$password = $_SERVER['HTTP_X_'.self::APPLICATION_ID.'_PASSWORD'];
// Find the user
$user=User::model()->find('LOWER(username)=?',array(strtolower($username)));
if($user===null) {
// Error: Unauthorized
$this->_sendResponse(401, 'Error: User Name is invalid');
} else if(!$user->validatePassword($password)) {
// Error: Unauthorized
$this->_sendResponse(401, 'Error: User Password is invalid');
}
} // }}}
// {{{ _getObjectEncoded
/**
* Returns the json or xml encoded array
*
* @param mixed $model
* @param mixed $array Data to be encoded
* @access private
* @return void
*/
private function _getObjectEncoded($model, $array)
{
if(isset($_GET['format']))
$this->format = $_GET['format'];
if($this->format=='json')
{
return CJSON::encode($array);
}
elseif($this->format=='xml')
{
$result = '<?xml version="1.0">';
$result .= "\n<$model>\n";
foreach($array as $key=>$value)
$result .= " <$key>".utf8_encode($value)."</$key>\n";
$result .= '</'.$model.'>';
return $result;
}
else
{
return;
}
} // }}}
// }}} End Other Methods
}
/* vim:set ai sw=4 sts=4 et fdm=marker fdc=4: */
?>
how make this can give example code pre one table
case ‘Country’: // {{{
$models = Country::model()->findAll();
break; // }}}
i’ll send more data if you’ll want please thank you
how send data using web service like above tutorial