服务器端的CURL HTTP验证 [英] CURL HTTP Authentication at server side

查看:160
本文介绍了服务器端的CURL HTTP验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Zend构建Web服务,我必须在发送用户响应之前对其进行身份验证。用户将使用curl将请求发送到服务器页面,以 curl_setopt($ curl,CURLOPT_USERPWD,'key:pass');

Iam使用Zend框架,所以服务器端页面表示为:



http://www.example.com/app_name/public/controller/action/parameter



以下是用户请求的总代码(client.php):

 <?php 
$ curl = curl_init('http://www.example.com/app/public/user/add/1');

curl_setopt($ curl,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ curl,CURLOPT_USERPWD,'username:password');
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);
}

?>

现在我想要的是,我必须能够在服务器端检索用户名和密码(在我的zend控制器),以便我可以验证用户凭据从数据库,并发送相应的响应。那么如何在另一个页面中进行身份验证?

解决方案

Zend Framework有一个用于处理HTTP身份验证的现成组件。 p>

查看

上的示例



如果你不想使用它,你仍然可以按照old-school的方式,如





并手动检查 $ _ SERVER ['PHP_AUTH_USER'] $ _ SERVER ['PHP_AUTH_PW']


I am building a web service with Zend and i have to authenticate the users before sending them response. The user will send a request to a server page, using curl, passing his credentials in the form of curl_setopt($curl, CURLOPT_USERPWD, 'key:pass');

Iam using Zend framework and so the server side page is denoted like:

http://www.example.com/app_name/public/controller/action/parameter

Here is the total code for user's request (client.php):

<?php
$curl = curl_init('http://www.example.com/app/public/user/add/1');

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);                         
curl_setopt($curl, CURLOPT_USERPWD, 'username:password');
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);                         
}

?>

Now what i want is that, i must be able to retrieve the username and password at the server side (in my zend controller), so that i can verify the user credentials from database and send the response accordingly. So how can i do authentication in another page?

解决方案

Zend Framework has a ready-made component for handling HTTP authentication.

Checkout the examples at

If you don't want to use that, you can still go the "old-school" way, as described in

and manually check on $_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW']

这篇关于服务器端的CURL HTTP验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆