使用Zend进行curl http身份验证

sha*_*nth 1 php curl zend-framework http

我使用Zend Rest Controller开发Web服务,现在我需要在用户访问我的页面之前对其进行身份验证.

为此,iam计划使用curl进行HTTP身份验证,如下所示:

curl_setopt($ch, CURLOPT_USERPWD, "myusername:mypassword"); 
Run Code Online (Sandbox Code Playgroud)

我称之为卷曲页面如下:

curl -u myusername:mypassword http://localhost/controller/action/page.xml
Run Code Online (Sandbox Code Playgroud)

我有一个很大的疑问.

我是否需要将用户名和密码存储在.htpasswd文件中?如果是这样,我如何检索参数并验证它们?

Dev*_*ris 8

如果你使用.htaccess,网络服务器将默默地为你处理一切.你可以假设(除非你犯了一个错误)该人被授权.

但是如果你想使用http auth并获得在你自己的登录系统中使用的用户名和密码,那么你需要编写被调用的脚本,如下所示.

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

除了发送用户名和密码curl真的与它无关.它就像一个http客户端,就像一个webbrowser一样.

你如何执行auth归结为你想要实现的目标.http auth是一个简单的身份验证系统.

DC

我给你的链接告诉你如何做到这一点.您正在调用的页面必须发回一个身份验证请求,否则curl将不会转发身份验证详细信息.

如果您使用.htaccess和.htpasswd文件,您将无法看到身份验证,因为Web服务器将删除这些标头.如果您正在使用我提供的链接中的自定义http身份验证,那么只有在发送身份验证请求后,您才能访问它们,这意味着第一次加载页面时您将无法获得它们.

我的意思是..首先curl要求没有任何凭据的页面,如果它获取页面然后停止,如果它获得凭据请求然后发送它们.

这意味着如果您想http://localhost/controller/action/page.xml查看凭据,那么它必须要求它们.如果它是一个看起来像它的静态页面,那么它将不会要求提供凭据.

你打算打字吗? http://localhost/controller/action/page.php

DC

过程就像这样......

curl                   ---> page.php (first request)
curl                   <--- page.php (first reply) "Please identify yourself" ('401 unauthorised' http code)
curl username:password ---> page.php (second request now includes user auth)
curl                   <--- page.php (second reply) ok here is the page (if id succesfull)
Run Code Online (Sandbox Code Playgroud)

好的,承诺一些工作的例子

首先创建2个PHP脚本

auth.php是我上面发布的链接提供的示例的修改副本

<?php

if (!isset($_SERVER['PHP_AUTH_USER'])) {
    header('WWW-Authenticate: Basic realm="My Realm"');
    header('HTTP/1.0 401 Unauthorized');
    echo "User name and password is required. Please try again.\n";
} else {
    // we now have access to the user and password :)
    echo "Hello {$_SERVER['PHP_AUTH_USER']}\n";
    echo "You entered {$_SERVER['PHP_AUTH_PW']} as your password.\n";
}

?>  
Run Code Online (Sandbox Code Playgroud)

curlfetch.php是您的其他帖子中的脚本副本

<?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);
}
?>
Run Code Online (Sandbox Code Playgroud)

在您的服务器上创建并保存这些脚本

现在测试脚本1没有密码

curl http://localhost/auth.php
Run Code Online (Sandbox Code Playgroud)

这导致......

User name and password is required. Please try again.
Run Code Online (Sandbox Code Playgroud)

现在尝试使用密码

curl -u user:pass http://localhost/auth.php
Run Code Online (Sandbox Code Playgroud)

这导致......

Hello user
You entered pass as your password.
Run Code Online (Sandbox Code Playgroud)

现在尝试第二个脚本,注意我们不提供脚本中提供的用户名或密码

curl http://localhost/curlfetch.php 
Run Code Online (Sandbox Code Playgroud)

这导致......

Hello key
You entered 123456 as your password.
Run Code Online (Sandbox Code Playgroud)

现在作为参考尝试从您的浏览器调用两个脚本

DC