Yii Framework Forum: New Yii REST Tutorial - Yii Framework Forum

Jump to content

  • (3 Pages)
  • +
  • 1
  • 2
  • 3
  • You cannot start a new topic
  • You cannot reply to this topic

New Yii REST Tutorial Rate Topic: ***** 3 Votes

#21 User is offline   mediawiz 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 11-April 12

Posted 11 April 2012 - 02:15 PM

Thanks for the great tutorial. My REST client for Firefox is returning the data from the database, but I need to initialize my data before I load app.js.
<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.
0

#22 User is offline   mediawiz 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 11-April 12

Posted 11 April 2012 - 02:58 PM

Sorry for responding to my own post, this works, but is there a better way?

<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>
0

#23 User is offline   sepdau 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 01-May 12

Posted 04 May 2012 - 07:21 PM

How to authentication
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
0

#24 User is offline   Jean-Marie 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 12-April 12
  • Location:Brittany, France.

Posted 07 May 2012 - 02:22 AM

View Postsepdau, on 04 May 2012 - 07:21 PM, said:

How to authentication
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.
1

#25 Guest_carpanta_*

  • Group: Guests

Posted 07 June 2012 - 08:15 PM

Hello everybody,

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.


View PostJean-Marie, on 07 May 2012 - 02:22 AM, said:

In RESTClient, use the 'headers' functionality:
* menu 'Headers'
* 'Custom Header'
* 'Name' : X_ASCCPE_PASSWORD
* 'Value' : emo

And the same with USERNAME.

HTH,

JM.

0

#26 User is offline   Jean-Marie 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 12-April 12
  • Location:Brittany, France.

Posted 08 June 2012 - 02:46 AM

View Postcarpanta, 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.
0

#27 Guest_carpanta_*

  • Group: Guests

Posted 08 June 2012 - 05:18 AM

Well really I don't know, how to know that user login. I have to create it? or is already in somewhere?

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



View PostJean-Marie, on 08 June 2012 - 02:46 AM, said:

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.

0

#28 User is offline   Jean-Marie 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 12-April 12
  • Location:Brittany, France.

Posted 08 June 2012 - 05:36 AM

View Postcarpanta, on 08 June 2012 - 05:18 AM, said:

But I can't figure out how to create a user. I am totally newbie with Yii as you can see.



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.
0

#29 Guest_carpanta_*

  • Group: Guests

Posted 08 June 2012 - 06:58 AM

Oh nice, now it works :) Thanks Jan Marie. Anyway I'm wondering now, coz I have created a new model for my appto manage users with gii tool, and my doubt right now is whether I need add a new model to do that or is expected a own yii model for Users modifying something in it?

Thanks again.

View PostJean-Marie, on 08 June 2012 - 05:36 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.

0

#30 User is offline   Jean-Marie 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 12-April 12
  • Location:Brittany, France.

Posted 08 June 2012 - 07:18 AM

View Postcarpanta, on 08 June 2012 - 06:58 AM, said:

Oh nice, now it works :) Thanks Jan Marie. Anyway I'm wondering now, coz I have created a new model for my appto manage users with gii tool, and my doubt right now is whether I need add a new model to do that or is expected a own yii model for Users modifying something in it?

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');
        	}


0

#31 User is offline   jaimes 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 09-June 12

Posted 09 June 2012 - 01:24 PM

Hi all,
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


View Postjwerner, on 15 April 2011 - 03:54 PM, said:

Hello,


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

0

#32 User is offline   3amsleep 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 27-May 12

Posted 09 June 2012 - 02:46 PM

THANKS!, I'm just starting on Yii and i was wondering how to implement a REST service on it. Thanks a lot man!
0

#33 User is offline   vladimircape 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 06-June 12

Posted 13 June 2012 - 03:18 AM

Also it will be helpfull if you describe how to do in such case
have 2 URL
site.ru/user/create(for HTML)
and site.ru/user/create.json(for JSON)
but only one controller should be (he will render HTML or JSON
yiif.ru - my site about yii
0

#34 User is offline   zeratool 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 20-September 12

Posted 17 November 2012 - 12:25 PM

Does anyone here tried this REST extension -- https://github.com/weavora/wrest ?

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.
0

#35 User is offline   13on 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 06-October 12

Posted 03 December 2012 - 06:37 AM

Help me please

I tried to do follow this

View PostJean-Marie, on 07 May 2012 - 02:22 AM, said:

In RESTClient, use the 'headers' functionality:
* menu 'Headers'
* 'Custom Header'
* 'Name' : X_ASCCPE_PASSWORD
* 'Value' : emo

And the same with USERNAME.

HTH,

JM.


View PostJean-Marie, on 08 June 2012 - 05:36 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.


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.
0

#36 User is offline   danialtehrani 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 29-December 12

Posted 16 March 2013 - 02:35 AM

Hi

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
0

#37 User is offline   Jean-Marie 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 12-April 12
  • Location:Brittany, France.

Posted 18 March 2013 - 01:05 AM

Hi,

check this : http://stackoverflow...478442#10478442 (or this : http://www.yiiframew...819#entry151819)

HTH,

JM.
0

#38 User is offline   AidanT 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 24-March 13

Posted 26 March 2013 - 12:25 PM

This is great, just what i've been looking for!
0

#39 User is offline   acy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 24-March 13

Posted 31 March 2013 - 07:07 AM

Again... Thank you so much for this tutorial!
0

#40 User is offline   frisor 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 06-April 13

Posted 06 April 2013 - 12:17 PM

Thanks for the tutorial. nice job thanks :)
0

Share this topic:


  • (3 Pages)
  • +
  • 1
  • 2
  • 3
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users