New Yii REST Tutorial
#21
Posted 11 April 2012 - 02:15 PM
<script type='text/javascript' src='<?php echo Yii::app()->request->baseUrl; ?>/scripts/js/jquery-1.7.1.min.js'><script>
<script type='text/javascript' src='<?php echo Yii::app()->request->baseUrl; ?>/scripts/js/underscore-min.js'></script>
<script type='text/javascript' src='<?php echo Yii::app()->request->baseUrl; ?>/scripts/js/backbone-min.js'></script>
<script>
var trucks = < I need to get the JSON string from ApiController via api/trucks >
</script>
<script type='text/javascript' src='<?php echo Yii::app()->request->baseUrl; ?>/scripts/js/app.js'></script>
Any help is appreciated.
#22
Posted 11 April 2012 - 02:58 PM
<script>
var trucks = [];
$.ajax({
url: "/index.php/api/trucks",
dataType: "json",
method: "GET",
async: false,
success: function(response){
trucks = response;
}
});
</script>
<script type='text/javascript' src='<?php echo Yii::app()->request->baseUrl; ?>/scripts/js/app.js'></script>
#23
Posted 04 May 2012 - 07:21 PM
I use RESTClient addon Firefox but I dont know how to set header of it.
I try Content-Type: application/json and body HTTP_X_ASCCPE_PASSWORD=emo&HTTP_X_ASCCPE_USERNAME =aaaaaaaaaaaa
But it not return true
#24
Posted 07 May 2012 - 02:22 AM
sepdau, on 04 May 2012 - 07:21 PM, said:
I use RESTClient addon Firefox but I dont know how to set header of it.
I try Content-Type: application/json and body HTTP_X_ASCCPE_PASSWORD=emo&HTTP_X_ASCCPE_USERNAME =aaaaaaaaaaaa
But it not return true
In RESTClient, use the 'headers' functionality:
* menu 'Headers'
* 'Custom Header'
* 'Name' : X_ASCCPE_PASSWORD
* 'Value' : emo
And the same with USERNAME.
HTH,
JM.
#25
Posted 07 June 2012 - 08:15 PM
Trying to run this example I didn't found the way to get success with it. I added the restclient addon on firefox and setted the parameters
Method: GET
URL: localhost/yiirest/index.php/api/posts
Headers (in menu Headers/Custom header)
X_ASCCPE_USERNAME: emo
X_ASCCPE_PASSWORD: emo
But I am getting this response:
Status Code: 401 Unauthorized
Connection: Keep-Alive
Content-Length: 27
Content-Type: text/html
Date: Fri, 08 Jun 2012 01:10:22 GMT
Keep-Alive: timeout=5, max=100
Server: Apache/2.2.21 (Win32) mod_ssl/2.2.21 OpenSSL/1.0.0e PHP/5.3.8 mod_perl/2.0.4 Perl/v5.10.1
X-Powered-By: PHP/5.3.8
Just trying to figure out how works Rest API, any help it's welcome.
Jean-Marie, on 07 May 2012 - 02:22 AM, said:
* menu 'Headers'
* 'Custom Header'
* 'Name' : X_ASCCPE_PASSWORD
* 'Value' : emo
And the same with USERNAME.
HTH,
JM.
#26
Posted 08 June 2012 - 02:46 AM
carpanta, on 07 June 2012 - 08:15 PM, said:
Status Code: 401 Unauthorized
Connection: Keep-Alive
Content-Length: 27
Content-Type: text/html
Date: Fri, 08 Jun 2012 01:10:22 GMT
Keep-Alive: timeout=5, max=100
Server: Apache/2.2.21 (Win32) mod_ssl/2.2.21 OpenSSL/1.0.0e PHP/5.3.8 mod_perl/2.0.4 Perl/v5.10.1
X-Powered-By: PHP/5.3.8
What is the body you get back?
The user emo:emo exists in you User table?
You could put some traces in code (Yii::trace()) and activate logging (see http://www.yiiframew.../topics.logging)
JM.
#27
Posted 08 June 2012 - 05:18 AM
In this web page you address me I didn't found the way to do it sorry.. and I have looking, and I have seen only for enable loggings in the /protected/main.php like this
'log'=>array(
...
'class'=>'CLogRouter',
'routes'=>array(
'weblogging'=>array(
'class'=>'CWebLogRoute',
'enabled'=>true,
),
),
),But I can't figure out how to create a user. I am totally newbie with Yii as you can see.
Thanks in andancce
Jean-Marie, on 08 June 2012 - 02:46 AM, said:
The user emo:emo exists in you User table?
You could put some traces in code (Yii::trace()) and activate logging (see http://www.yiiframew.../topics.logging)
JM.
#28
Posted 08 June 2012 - 05:36 AM
carpanta, on 08 June 2012 - 05:18 AM, said:
The user comes from a table in the database. Until you setup this, try following: in the function _checkAuth() (in the protected/controllers/ApiController.php file), that should look like this:
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');
}
} // }}}
insert following line before the '// Find the user' line:
if ($username === "emo" && $password === "emo") return;
JM.
#29
Posted 08 June 2012 - 06:58 AM
Thanks again.
Jean-Marie, on 08 June 2012 - 05:36 AM, said:
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');
}
} // }}}
insert following line before the '// Find the user' line:
if ($username === "emo" && $password === "emo") return;
JM.
#30
Posted 08 June 2012 - 07:18 AM
carpanta, on 08 June 2012 - 06:58 AM, said:
Thanks again.
Glad it works!
Sure, if you already have created some kind of user table, tweak the code so as to use your data:
Remove the line I told you to insert (was for test), and adapt the line
$user=User::model()->find('LOWER(username)=?',array(strtolower($username)));
to match your model.
Then modify the password validation (currently 'else if(!$user->validatePassword($password))') to whatever you need.
As an example (yours will probably differ), here is how I modified it (I'm using the Users extension):
$identity = new UserIdentity($username, $password);
$identity->authenticate();
if ($identity->errorCode === UserIdentity::ERROR_NONE) {
Yii::app()->user->login($identity, 0);
} else {
$this->_sendResponse(401, 'Invalid credentials');
}
#31
Posted 09 June 2012 - 01:24 PM
I just followed this tutorial. It's now working on my personal laptop(localhost) but when I try to upload it to the server (godaddy.com) I just doesn't work. I get an error that says 'no input file specified'. 404 Not Found. If i remove the urlFormat It just work perfectly, I means the controller and ActiveRecords ). Doesn't anyone knows why this it?
Thanks a lot.
j
jwerner, on 15 April 2011 - 03:54 PM, said:
I have written a tutorial about how to implement a REST API with Yii::
http://www.yiiframew...ate-a-rest-api/
Please post your feedback here!
Best Regards,
Joachim
#32
Posted 09 June 2012 - 02:46 PM
#34
Posted 17 November 2012 - 12:25 PM
I found the syntax very neat. Like you, i want to develop a RESTful app with YII.
Any feedback on this extension is really appreciated.
Thanks.
#35
Posted 03 December 2012 - 06:37 AM
I tried to do follow this
Jean-Marie, on 07 May 2012 - 02:22 AM, said:
* menu 'Headers'
* 'Custom Header'
* 'Name' : X_ASCCPE_PASSWORD
* 'Value' : emo
And the same with USERNAME.
HTH,
JM.
Jean-Marie, on 08 June 2012 - 05:36 AM, said:
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');
}
} // }}}
insert following line before the '// Find the user' line:
if ($username === "emo" && $password === "emo") return;
JM.
But it return
Status Code: 401 Unauthorized
Connection: Keep-Alive
Content-Length: 795
Content-Type: text/html
Date: Mon, 03 Dec 2012 11:27:53 GMT
Keep-Alive: timeout=5, max=100
Server: Apache/2.4.2 (Win32) OpenSSL/1.0.1c PHP/5.4.4
X-Powered-By: PHP/5.4.4
When I didn't use $this->_checkAuth();
It's work. I don't know how can I fix it.
I've never used web service before, it's first time for me.
Thank a lot.
#36
Posted 16 March 2013 - 02:35 AM
I'm using RESTClient Firefox Add-ons
I've set Custom Header.
Name: HTTP_X_ASCCPE_USERNAME
Password: demo
and again another custom header with:
Name: HTTP_X_ASCCPE_PASSWORD
Password: demo
I'm sure that a user with username=demo and password=demo is exist in Database, but I've got this error:
Status Code: 401 Unauthorized
Connection: Keep-Alive
Content-Length: 762
Content-Type: text/html
Date: Sat, 16 Mar 2013 07:34:04 GMT
Keep-Alive: timeout=5, max=100
Server: Apache/2.4.2 (Win64) PHP/5.4.3
X-Powered-By: PHP/5.4.3
What should I do?
Thanks a lot
#37
Posted 18 March 2013 - 01:05 AM
check this : http://stackoverflow...478442#10478442 (or this : http://www.yiiframew...819#entry151819)
HTH,
JM.

Help













