php - curl http authentication with Zend -


i used zend rest controller developing web service, , need authenticate users before can access page.

for this, iam planning use http authentication curl, this:

curl_setopt($ch, curlopt_userpwd, "myusername:mypassword");  

and call curl page this:

curl -u myusername:mypassword http://localhost/controller/action/page.xml 

i have big doubt.

do need store usernames , passwords in .htpasswd file? if so, how need retrieve parameters , validate them ?

if use .htaccess webserver silently handle you. can assume (unless have made mistake) person authorised.

but if want use http auth , user name , password use in own login system need write script thats being called shown here.

http://php.net/manual/en/features.http-auth.php

apart sending through username , password curl has nothing it. acts http client, webbrowser is.

how perform auth comes down trying achieve. http auth simple authentication system.

dc

the link gave shows how it. page calling must send authentication request otherwise curl not forward authentication details.

if use .htaccess , .htpasswd file not see authentication because webserver remove headers. if using custom http auth 1 in link provided access them after auth request sent, means first time page loaded won't them.

what mean is.. first curl asks page without credentials, if gets page stops, if gets request credentials sends them.

this means if want http://localhost/controller/action/page.xml see credentials must ask them. if static page looks is, not ask credentials.

did mean type http://localhost/controller/action/page.php

dc

the process goes this...

curl                   ---> page.php (first request) curl                   <--- page.php (first reply) "please identify yourself" ('401 unauthorised' http code) curl username:password ---> page.php (second request includes user auth) curl                   <--- page.php (second reply) ok here page (if id succesfull) 

ok promised working examples

first create 2 php scripts

auth.php modified copy of example provided link posted above

<?php  if (!isset($_server['php_auth_user'])) {     header('www-authenticate: basic realm="my realm"');     header('http/1.0 401 unauthorized');     echo "user name , password required. please try again.\n"; } else {     // have access user , password :)     echo "hello {$_server['php_auth_user']}\n";     echo "you entered {$_server['php_auth_pw']} password.\n"; }  ?>   

curlfetch.php copy of script other post

<?php  $curl = curl_init('http://localhost/auth.php'); curl_setopt($curl, curlopt_returntransfer, 1); curl_setopt($curl, curlopt_userpwd, 'key:123456'); curl_setopt($curl, curlopt_httpauth, curlauth_any); curl_setopt($curl, curlopt_ssl_verifypeer, false); curl_setopt($curl, curlopt_followlocation, true); curl_setopt($curl, curlopt_useragent, 'sample code');  $response = curl_exec($curl); $resultstatus = curl_getinfo($curl);  if($resultstatus['http_code'] == 200) {     echo $response; } else {     echo 'call failed '.print_r($resultstatus); } ?> 

create , save scripts on server

now test script 1 without password

curl http://localhost/auth.php 

this results in...

user name , password required. please try again. 

now try password

curl -u user:pass http://localhost/auth.php 

this results in...

hello user entered pass password. 

now try second script, note don't supply username or password supplied within script

curl http://localhost/curlfetch.php  

that results in...

hello key entered 123456 password. 

now reference try calling both scripts browser

dc


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -