我正在尝试为旧 API 创建自定义身份验证适配器。我们称之为适配器TR42
。现在,我正在调试TR42::check()
,所以我使用的是硬编码值:
<?php
class TR42 extends \lithium\core\Object {
public function __construct(array $config = []) {
$defaults = [
'scheme' => 'http',
'host' => 'localhost/tr42/mock_api_authenticate.php',
'action' => 'authLookup',
'fields' => ['username', 'password'],
'method' => 'POST'
];
parent::__construct($config + $defaults);
}
public function check($credentials, array $options = []) {
$postConfig = [
/**
* Should I be using 'body' or 'query' to submit POST fields?
*/
'body' => [
'username' => 'housni',
'password' => sha1('legacyHashedPassword'),
'action' => 'authLookup'
],
'query' => 'username=housni&password=' . sha1('legacyHashedPassword') . '&action=authLookup',
];
$request = new Request($postConfig + $this->_config);
$stream = new Curl($this->_config);
$stream->open();
$stream->write($request);
$response = $stream->read();
$stream->close();
echo '<pre>' . print_r($response, true) . '</pre>';
die();
}
}
?>
该文件http://localhost/tr42/mock_api_authenticate.php
如下所示:
<?php
echo '<h1>This request is: ' . $_SERVER['REQUEST_METHOD'] . '</h1>';
if (!empty($_POST)) {
echo '<h1>YAY</h1>';
} else {
echo '<h1>NAY</h1>';
}
echo '<pre>' . print_r($_POST, true) . '</pre>';
?>
由于我的 cURL 代码提交了一个 POST 请求,我希望我的 $_POST 被填充,mock_api_authenticate.php
但这并没有发生,因为 TR42::check() 的 print_r() 的输出是:
HTTP/1.1 200 OK
Date: Sun, 18 Aug 2013 04:46:34 GMT
Server: Apache/2.2.14 (Ubuntu)
X-Powered-By: PHP/5.4.17-1~lucid+1
Vary: Accept-Encoding
Content-Length: 63
Connection: close
Content-Type: text/html
This request is: POST
NAY
Array
(
)
它说请求是 POST 但 POST 数组(最后一个空数组)是空的。
我究竟做错了什么?
谢谢阅读 :)